Search code examples
pythonpython-3.xpip

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

  • 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).