I’m trying to incrementally adopt pants in a proje...
# general
l
I’m trying to incrementally adopt pants in a project that currently defines all of its dependencies in setup.py (
install_requires
,
extras_require
, etc.) Does pants offer a way to generate
python_requirement
targets for my third party dependencies from the existing setup.py file? Similar to the `python_requirements`/`poetry_requirements` generators?
h
Interesting idea, there is no such way at the moment.
Getting at those dependencies in a principled way would require running the setup.py I assume
Is there a standard setuptools command for printing those out I wonder
l
f
If you have a variable for
install_requires
et al. which is set in the script before being passed into the
setup
function, you can
exec
the file and store the local variables into a dictionary. You could do that in a plugin, probably, or at least print them out without requiring deprecated distutils
I’ve used that technique to read a
__version__
out of
__init__.py
without having to import the package in question
h
Here's what I did
Copy code
def gather_install_requires() -> List[str]:
    req_file = pathlib.Path(__file__).parent / 'requirements.txt'
    return [
        req for req in req_file.read_text().splitlines()
        if not (req.startswith('#') or 'git+ssh' in req)
    ]
Then I just call that function inside
setup.py
like
install_requires=gather_install_requires()
. Hacky thing is removing some requirements for our internal repos that we list in that file.
Then I can have a setup file like I want and a separate requirements file that pants knows how to use. No plugin necessary.
h
An alternative might be to use setup.cfg (https://docs.python.org/3/distutils/configfile.html) which is the “standard” for declarative versions of the programmatic stuff in setup.py
I don’t know how commonly setup.cfg is used
And we’d need a target generator for it, but that would be very easy to write