'Get python3 package metadata given source directory without installing
I am trying to get some package metadata (name, version) given a path to the source directory without installing said package.
These work, using setup.py if you're sitting in the root directory:
> python3 setup.py --name
my_package_name
> python3 setup.py --version
0.1.0
However, I have been cautioned away from using python3 setup.py commands -- and indeed see a warning:
.../lib/python3.7/site-packages/setuptools/installer.py:30: SetuptoolsDeprecationWarning: setuptools.installer is deprecated. Requirements should be satisfied by a PEP 517 installer.
SetuptoolsDeprecationWarning,
I know pip show my_package_name will print various metadata about a package (including name/version), however it requires that the package is installed into the environment. It also doesn't take a source directory and thus requires I already know the name of the package I want the info on.
> pip show .
WARNING: Package(s) not found: .
> pip show my_package_name
WARNING: Package(s) not found: my_package_name
> pip install .
...
> pip show my_package_name
Name: my_package_name
Version: 0.1.0
...
...
Is there any equivalent pip command (or other tool) that will show me the version of a package given the source directory without actually installing the package?
Thanks in advance!
Solution 1:[1]
You can use pip -v install together with --global-option="--version". According to the docs this will translate in the following way:
python -m pip -v install --global-option="--version" path/to/project
is equivalent to
python setup.py --version install
The latter command just runs the --version part and then exits, thus ignoring the install part. Since the corresponding subprocess terminates successfully, pip will report it has installed the package when it actually didn't. Anyway, the third line from the bottom will contain the output from the setup.py call, i.e. the version number:
Processing ./path/to/project
Running command python setup.py egg_info
running egg_info
[...]
Preparing metadata (setup.py) ... done
Skipping wheel build for testpkg, due to binaries being disabled for it.
Installing collected packages: testpkg
Running command Running setup.py install for testpkg
1.2
Running setup.py install for testpkg ... done
Successfully installed testpkg
Note: This is probably a hacky solution as it relies on the printed output of pip -v install which might change for future releases. Anyway, it works for the current version of pip (22.0.4).
Solution 2:[2]
It isn't as compact as the setup.py calls, but you can use the pep517 package.
python -m pep517.meta my-package will create a my-package/dist/my_package.dist-info/METADATA file where you can read all the available info.
pip vendors this PyPA library so python -m pip._vendor.pep517.meta my-package should work without additional installs.
Caveat
This will only work if the PEP-517 build backend has implemented the prepare_metadata_for_build_wheel hook, which is optional according to https://peps.python.org/pep-0517/#prepare-metadata-for-build-wheel
Lets see if someone comes up with a better tool.
If you ever run on a pure PEP-517 project using the setuptools.build_meta backend that lacks a setup.py file, you can still create an "empty" one (with the following content)
from setuptools import setup
setup()
and keep using the python setup.py instructions.
BUT this won't work for other PEP-517 build backends like poetry or flit.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | a_guest |
| Solution 2 |
