thebjorn
thebjorn

Reputation: 27311

setup.py command to show install_requires?

Is there any way to get the list of packages specified in the install_requires parameter to the setup command (in setup.py)?

I'm looking for something similar to pip show pkgname | grep -i requires, but for local packages (and that reports version specifiers and filters).

The real task I'm solving is to check if versions specified in setup.py and requirements.txt have diverged, so if there is a tool that can do this directly...?

Upvotes: 2

Views: 622

Answers (1)

wim
wim

Reputation: 362716

For installed packages, use stdlib importlib.metadata.requires. Python 3.8+ is required, but there is a backport importlib-metadata for older Python versions. This is the easy case, because installed packages have already generated the metadata (including dependency specs) at installation time. For local source trees which aren't necessarily installed, read on.

There is a way to do this in Python APIs via distutils (which setuptools wraps):

import distutils.core
dist = distutils.core.run_setup("setup.py")
for req in dist.install_requires:
    print(req)

As a shell command that might look like:

python -c 'import distutils.core; print(*distutils.core.run_setup("setup.py").install_requires, sep="\n")'

It is also possible to use the CLI to generate egg-info and then read deps from that:

python setup.py egg_info
cat myproject.egg-info/requires.txt

However, distutils is deprecated and setuptools is trying to be a library only, so using setup.py in this way is quite neglected.

You might consider moving to declarative dependencies specified in pyproject.toml and then just reading the requirements directly with a simple TOML parser.

Upvotes: 2

Related Questions