-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Make Distribution._dep_map
into a public attribute
#2116
Comments
I'd recommend instead of using pkg_resources, to use |
I guess I could just use output of pkg = pkg_resources.get_distribution("MyPackage")
installed_extras = []
for extra in pkg.extras:
try:
pkg_resources.require(pkg.requires(extras=[extra]))
except DistributionNotFound:
pass
else:
installed_extras.append(extra) I guess I'm just surprised it's not really possible (in a supported way that is) to only get requirements of extra without requirements of main package. |
Totally fair. I'd recommend passing the results of I do agree, it's annoying there isn't something more sophisticated to achieve what you need, but that's the world we live in for now. |
Problem with using import importlib.metadata
import packaging.markers
import packaging.requirements
import pkg_resources
dist = importlib.metadata.distribution("MyPackage")
# funnily, ↓ this line isn't part of stable API as it's a method of email.Message
extras = dist.metadata.get_all("Provides-Extra")
raw_reqs = dist.requires
parsed_reqs = list(
filter(
lambda req: req.marker is not None, # if no marker, it can't be extra req
(packaging.requirements.Requirement(raw) for raw in raw_reqs)
)
)
installed_extras = []
for extra in extras:
extra_reqs = []
for req in parsed_reqs:
try:
req.marker.evaluate()
except packaging.markers.UndefinedEnvironmentName:
# if above doesn't raise an error, it means it isn't extra req
if req.marker.evaluate(environment={"extra": extra}):
extra_reqs.append(req.name)
try:
pkg_resources.require(extra_reqs)
except pkg_resources.DistributionNotFound:
pass
else:
installed_extras.append(extra) I tested a lot of stuff before making this issue 😄 Anyway, I think it's great that all those APIs for packaging exist in Python, even if I can't do everything with them in a way I want to. I currently use the |
Yikes. That's clumsy. I'd recommend exposing that functionality in importlib.metadata, except that it's a low-level interface and so doesn't have access to the classes in packaging. Similarly, I don't think packaging has an interest in having a dependency on importlib.metadata, so it seems there's a need for a package that sits above those and provides interfaces that consume both. In pypa/packaging-problems#317, I'm trying to resolve this problem. Short answer is that I'm trying to supplant the functionality in pkg_resources, so I'd rather not expose more functionality to deprecate, but this use-case helps the PyPA understand the need and hopefully there will be a better solution soon. |
Would it be possible to make
Distribution._dep_map
frompkg_resources
API public attribute? It seems quite useful to be able to know which extra has what requirements, for example - to determine what extras of the package are installed:The text was updated successfully, but these errors were encountered: