-
Notifications
You must be signed in to change notification settings - Fork 991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse dependencies from Python setup.cfg files #2281
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Best to avoid using distutils, we're trying to actively deprecate it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so I take it that I should close this PR?
Is there an alternative best practice for getting the dependencies of a Python package which uses
setuptools
, no matter whether the dependencies are listed insetup.py
orsetup.cfg
?If there is, I'd like to add it as an answer to this Stack Overflow question which is one of the top search results when looking for solutions to read a Python package's dependencies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This question is actually a bit difficult to answer. Assuming you actually just want to get the dependencies, for
setuptools
projects, the biggest issue you'll have is that many projects unfortunately don't know about (or haven't updated to use) environment markers, so it's distressingly common to see stuff like this:E.g. from s3transfer. This means that depending on the machine where you run
setup.py
, you will get a different answer for the values ofinstall_requires
. The correct way to do this is to declare a fixed set of dependencies like so:In which case even the conditional dependencies will be included in the wheel metadata.
Assuming that you are comfortable with accepting "whatever will be included in the wheel if I were to build it in whatever worker environment" (seems reasonable), then I would say that the best thing to do is to implement enough of a PEP 517/518 backend to execute prepare_metadata_for_build_wheel, and parse the dependency metadata from the
.dist-info/METADATA
file. Thepep517
library should make this easy enough.If the project doesn't have a
pyproject.toml
file, I recommend defaulting to usebuild-backend=setuptools.build_meta:__legacy__
andrequires = ["setuptools >= 40.8.0", "wheel"]
, as we're doing in the python-build frontend. I think the majority of setuptools projects will work properly with these defaults. You can also fall back to usingpip wheel
to generate an actual wheel file and then extract the metadata from there, though that will consume more resources than necessary.Using PEP 517 also has the advantage that it works automatically with any PEP 517 backend, not just
setuptools
. (We in the PyPA would also probably not be unhappy if you said, "We only support packages that work well with PEP 517", as a further spur to get people to actively adopt the new build system).That said, I'm not exactly sure why you want this sort of generic information about a package's depdencies — my understanding of dependabot is that it is supposed to update your dependencies, no? So don't you need to know not only what they are, but also exactly where they are defined, so that you can make a PR to automatically update them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHub's Dependency Graph is implemented using dependabot.The way I arrived at this issue is that I wanted to simply see my project's dependencies in the GitHub UI and was surprised to see this message instead:So GitHub's Dependency Graph simply doesn't work for any Python packages which declare
setup_requires
insetup.cfg
instead ofsetup.py
.I then found the tip to use
distutils.core.run_setup(..., stop_after="init").install_requires
and figured that dependabot could do that, and also as a side effect gain a possibly more robust way to extract dependencies fromsetup.py
files than the current parse_setup() method which modifiessetup.py
andexec()
s it in a patched Python environment.parse_setup()
could still be used as a fallback for those cases whererun_setup()
fails.It sounds like a great idea to make GitHub's Dependency Graph only work on modern well-formed Python packages. It should indeed encourage authors to adopt good practices in packaging. The current situation actually does the reverse – it discourages adopting e.g. a declarative
setup.cfg
andpyproject.toml
and favors old-style packages with a traditional executablesetup.py
Update: @jurre noted below that GitHub's Dependency Graph actually isn't based on dependabot at all. I must have misunderstood some information I've read about the Dependency Graph.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still eager to help make GitHub's Dependency Graph work for modern Python repositories. @pganssle could you advise what would be the most efficient and acceptable way for me to contribute?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this, but I'm a complete Python noob, and I'm running into a bunch of missing import errors that I cannot seem to solve (on Python 3.6). So what I'd need it a complete example / standalone
.py
file that takes a directory / file as an argument and works out the box 🙄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sschuberth what errors are you getting? Python 3.6 is EOL: you should try this on a recent version of python eg 3.9 or 3.10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm stuck with Python 3.6, and I'm getting
NameError
forpathlib
andimportlib
not being defined,AttributeError: module 'importlib' has no attribute 'metadata'
after adding imports etc. I'll give https://stackoverflow.com/a/71276197/1127485 a try now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For older versions, you have to install and use the backport
importlib_metadata
(notice the underscore)