python / importlib_metadata

Library to access metadata for Python packages
https://importlib-metadata.readthedocs.io
Apache License 2.0
123 stars 80 forks source link

How to get ImportPackages from DistributionPackages metadata? #423

Closed buhtz closed 1 year ago

buhtz commented 1 year ago

I have an Distribution Package named bitdemo.

Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> from importlib.metadata import metadata
>>> m = metadata('bitdemo')

>>> m._headers
[('Metadata-Version', '2.1'), ('Name', 'bitdemo'), ('Version', '0.0.0'), ('Requires-Python', '>=3.8'), ('Requires-Dist', 'PySimpleGUI')]

That package do contain two ImportPackages. How can I get their name?

The relevant part of the `pyproject.toml` looks like this:
[project]
name = "bitdemo"
version = 0.0.1
requires-python = ">=3.8"
dependencies = [
    "PySimpleGUI"
]
[tool.setuptools.package-dir]
bitcli = "src/bitcli"
bitgui = "src/bitgui"
jaraco commented 1 year ago

You may be able to infer the names from importlib.metadata.files('bitdemo'). I was going to demo it on bitdemo, but that appears to be a private package. Here's an example with Setuptools:

 ~ $ pip-run setuptools -- -c "import importlib.metadata; print(set(str(file).partition('/')[0] for file in importlib.metadata.files('setuptools')))"
{'setuptools', 'distutils-precedence.pth', 'pkg_resources', 'setuptools-67.6.1.dist-info', '_distutils_hack'}
buhtz commented 1 year ago

Thanks for your feedback.

"Bit-demo" is well public on a real free server.