-
Notifications
You must be signed in to change notification settings - Fork 37
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
Solution to check if a requirement is met without pkg_resources #664
Comments
pkg_resources is deprecated and, under some circumstances, causes import errors in Python 3.12. This commit replaces the one remaining function from pkg_resources, the require() function, with a new implementation that does the same thing. It was necessary to implement the function anew because there's no equivalent for require() in the packaging or importlib.* modules. There is an implementation in the hbutils package, but I didn't think it was worth bringing in a new dependency for just this one function. https://hansbug.github.io/hbutils/main/api_doc/system/python.html#check-reqs See also pypa/packaging-problems#664 This new implementation is based most closely on the original pkg_resources code.
pkg_resources is deprecated and, under some circumstances, causes import errors in Python 3.12. This commit replaces the one remaining function from pkg_resources, the require() function, with a new implementation that does the same thing. It was necessary to implement the function anew because there's no equivalent for require() in the packaging or importlib.* modules. There is an implementation in the hbutils package, but I didn't think it was worth bringing in a new dependency for just this one function. https://hansbug.github.io/hbutils/main/api_doc/system/python.html#check-reqs See also pypa/packaging-problems#664 This new implementation is based most closely on the original pkg_resources code.
pkg_resources is deprecated and, under some circumstances, causes import errors in Python 3.12. This commit replaces the one remaining function from pkg_resources, the require() function, with a new implementation that does the same thing. It was necessary to implement the function anew because there's no equivalent for require() in the packaging or importlib.* modules. There is an implementation in the hbutils package, but I didn't think it was worth bringing in a new dependency for just this one function. https://hansbug.github.io/hbutils/main/api_doc/system/python.html#check-reqs See also pypa/packaging-problems#664 This new implementation is based most closely on the original pkg_resources code.
pkg_resources is deprecated and, under some circumstances, causes import errors in Python 3.12. This commit replaces the one remaining function from pkg_resources, the require() function, with a new implementation that does the same thing. It was necessary to implement the function anew because there's no equivalent for require() in the packaging or importlib.* modules. There is an implementation in the hbutils package, but I didn't think it was worth bringing in a new dependency for just this one function. https://hansbug.github.io/hbutils/main/api_doc/system/python.html#check-reqs See also pypa/packaging-problems#664 This new implementation is based most closely on the original pkg_resources code.
pkg_resources is deprecated and, under some circumstances, causes import errors in Python 3.12. This commit replaces the one remaining function from pkg_resources, the require() function, with a new implementation that does the same thing. It was necessary to implement the function anew because there's no equivalent for require() in the packaging or importlib.* modules. There is an implementation in the hbutils package, but I didn't think it was worth bringing in a new dependency for just this one function. https://hansbug.github.io/hbutils/main/api_doc/system/python.html#check-reqs See also pypa/packaging-problems#664 This new implementation is based most closely on the original pkg_resources code.
pkg_resources is deprecated and, under some circumstances, causes import errors in Python 3.12. This commit replaces the one remaining function from pkg_resources, the require() function, with a new implementation that does the same thing. It was necessary to implement the function anew because there's no equivalent for require() in the packaging or importlib.* modules. There is an implementation in the hbutils package, but I didn't think it was worth bringing in a new dependency for just this one function. https://hansbug.github.io/hbutils/main/api_doc/system/python.html#check-reqs See also pypa/packaging-problems#664 This new implementation is based most closely on the original pkg_resources code.
pkg_resources is deprecated and, under some circumstances, causes import errors in Python 3.12. This commit replaces the one remaining function from pkg_resources, the require() function, with a new implementation that does the same thing. It was necessary to implement the function anew because there's no equivalent for require() in the packaging or importlib.* modules. There is an implementation in the hbutils package, but I didn't think it was worth bringing in a new dependency for just this one function. https://hansbug.github.io/hbutils/main/api_doc/system/python.html#check-reqs See also pypa/packaging-problems#664 This new implementation is based most closely on the original pkg_resources code.
pkg_resources is deprecated and, under some circumstances, causes import errors in Python 3.12. This commit replaces the one remaining function from pkg_resources, the require() function, with a new implementation that does the same thing. It was necessary to implement the function anew because there's no equivalent for require() in the packaging or importlib.* modules. There is an implementation in the hbutils package, but I didn't think it was worth bringing in a new dependency for just this one function. https://hansbug.github.io/hbutils/main/api_doc/system/python.html#check-reqs See also pypa/packaging-problems#664 This new implementation is based most closely on the original pkg_resources code.
pkg_resources is deprecated and, under some circumstances, causes import errors in Python 3.12. This commit replaces the one remaining function from pkg_resources, the require() function, with a new implementation that does the same thing. It was necessary to implement the function anew because there's no equivalent for require() in the packaging or importlib.* modules. There is an implementation in the hbutils package, but I didn't think it was worth bringing in a new dependency for just this one function. https://hansbug.github.io/hbutils/main/api_doc/system/python.html#check-reqs See also pypa/packaging-problems#664 This new implementation is based most closely on the original pkg_resources code.
@HansBug Hi! I'm currently trying to get some information about how to best detect & handle missing extras / optional dependencies at runtime into the PyPA's packaging guide (pypa/packaging.python.org#1606) and your The current draft links to both this issue and your Not saying that it should be done now, because it's still possible that PyPA's reviewers will decide the guide shouldn't include this information at all, in which case it'd be pointless effort - but I thought I'd ask here & give a heads-up before making promises over there. |
Maybe distlib can do this. Within the Python packaging ecosystem, it is a relatively well-known and definitely well-trusted library. If it can not do it out of the box, maybe the amount of custom code necessary would be smaller than what would be necessary when using the combination of |
I had a look at from distlib.locators import DistPathLocator, DependencyFinder
from distlib.database import DistributionPath
req = "typer-slim[standard]"
distpath = DistributionPath(include_egg=True)
locator = DistPathLocator(distpath)
print(locator.locate(req)) But unfortunately it seems to completely ignore the extra in the requirement and always returns a positive result whether it's installed or not. I then tried dfin = DependencyFinder(locator)
print(dfin.find(req)) But this goes in the other direction and reports anything as unsatisfied that's part of any extra, whether the extra is part of the requirement or not, plus some other weird stuff like dependencies whose marker is not even fulfilled... |
I asked: pypa/distlib#234. |
By the way, another reason why even if this functionality was in |
Good point |
@HansBug Another question: It's probably implied by you posting this snippet here for others to use, but just to be sure: Is it fine to include it (or modified versions of it) in the aforementioned guide for people to copy into their code without the restrictions imposed by the Apache 2 license under which |
Yes, and that includes the launchers which are used on Windows only - those are around 750K in size. I can look at the issue @sinoroc raised on distlib, but if the size is going to be a deal-breaker for your needs, might you not use it, anyway? |
It would depend on the use case, e.g. if the optional dependency in question is NumPy or PyTorch, pulling in a 500 kB dependency should be worth it to make the experience better. But just going by the description of distlib's purpose from its README stating
it doesn't sound like the natural place for such functionality, which would be meant to be used in arbitrary packages, not just packaging tools. |
I posted a possible solution on the distlib issue. |
Problem description
In the previous
pkg_resources
package, this issue can be solved with the following code (link):However,
pkg_resources
has been deprecated, so this method is not recommended for continued use. In the migration guide provided in theimportlib_metadata
documentation, there is no new interface that is completely equivalent topkg_resources.require
. Therefore, we need to use theimport_metadata
library (native in Python 3.8 or higher) and thepackaging
library to achieve this functionality.I have currently implemented a version, the code is here: https://github.com/HansBug/hbutils/blob/main/hbutils/system/python/package.py#L202, where the most critical part is the
_yield_reqs_to_install
function, which checks a requirement and its sub-requirements included in its extra one by one, and enumerates the unsatisfied requirements. By calling this iterator function, the check of whether a requirement is satisfied can be achieved. This is the core code of the function:Of course, if you need this function immediately, you can simply
pip install hbutils>=0.9.0
and then
hbutils
is a universal toolkit library containing various common functions, and the project address is https://github.com/HansBug/hbutils. It will be maintained for a long time.The text was updated successfully, but these errors were encountered: