5

Is there any way to tell pip to skip some setup_requires dependencies when not needed?

In my scenario, I have pytest-runner declared as a setup dependency (which is used to execute tests) in my setup.py. When I try to install my package (I.e: pip install my-package.tar.gz) it downloads pytest-runner or it fails it is not available (*).

In setup.py I have:

...
setup_requires=['pytest-runner', 'flake8']
...

I would like to tell setup.py only to use pytest-runner only when executing tests. Is that possible?

As @deceze suggested, this declaration will work:

setup_requires=['pytest-runner', 'flake8'] if 'test' in sys.argv else []

But I don't want to add logic to setup.py.

(*) The environment is very restricted, that's why downloading a dependency is a roadblock.

2 Answers 2

3

It makes sense to declare that as an extra:

setup(
    ...,
    extras_require=dict(
        tests=[
            'pytest-runner'
        ]
    )
)

You specifically install that with pip install my-package[tests].

As an alternative: setup.py is a fully functional Python program… if you can detect your environment somehow, you can dynamically decide whether to add certain dependencies to the requires list or not.

Sign up to request clarification or add additional context in comments.

2 Comments

Declaring them as extra wouldn't work because they won't be available when executing python setup.py test and I don't want to install them locally. Checking the environment is definitely an option, I hope not the only one.
pip install my-package[tests] is really cool! Thanks for suggesting
1

rwt (Run With This) seems to be the preferred way of doing this.

It provides on-demand dependency resolution, making packages available for the duration of an interpreter session. One of its aimed scenarios is: test runners.

After installing rwt (*), you can load setup.py commands from pytest-runner module executing:

rwt pytest-runner -- setup.py test

or

python -m rwt pytest-runner -- setup.py test

(*) You still have to install an unrequired dependency (i.e: rwt), but it seems to have less impact than installing others.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.