50

When I run python setup.py test the dependencies listed in tests_require in setup.py are downloaded to the current directory. When I run python setup.py install, the dependencies listed in requires are instead installed to site-packages.

How can I have those tests_require dependencies instead installed in site-packages?

bignose
  • 30,281
  • 14
  • 77
  • 110
Danny Navarro
  • 2,733
  • 1
  • 18
  • 22
  • 6
    setuptools `tests_require` is [deprecated since 41.5.0](https://setuptools.readthedocs.io/en/latest/setuptools.html) (October 2019). We are now at 50.3.0. – Martin Thoma Sep 18 '20 at 08:24
  • 1
    Hello @MartinThoma is there an alternative to specify requirements needed for dev/test but not plain usage? – Danny Staple Feb 01 '21 at 13:57
  • 3
    That would be something like `extras_require={'test': ['pytest']}` in setup.py I think. – Danny Staple Feb 01 '21 at 14:04

3 Answers3

54

You cannot specify where the test requirements are installed. The whole point of the tests_require parameter is to specify dependencies that are not required for the installation of the package but only for running the tests (as you can imagine many consumers might want to install the package but not run the tests). If you want the test requirements to be included during installation, I would include them in the install_requires parameter. For example:

test_requirements = ['pytest>=2.1', 'dingus']
setup(
    # ...
    tests_require = test_requirements,
    install_requires = [
        # ... (your usual install requirements)
    ] + test_requirements,
)

As far as I know, there's no parameter you can pass to force this behavior without changing the setup script.

bignose
  • 30,281
  • 14
  • 77
  • 110
Jason R. Coombs
  • 41,115
  • 10
  • 83
  • 93
  • 3
    I found [this article](http://reinout.vanrees.org/weblog/2009/12/17/managing-dependencies.html) which describes using 'extras' for a similar purpose. – Jason R. Coombs Oct 12 '11 at 22:03
  • 4
    I no longer recommend this technique. If tests are required for installation (whether or not they're required for tests), just put them in `install_requires`, and don't bother adding them to `tests_require`. – Jason R. Coombs Oct 06 '19 at 15:32
28

You can use a virtualenv to avoid this and install the extra packages to their default locations, inside lib/pythonX/site-packages. First, you should define your testing requirements as extras, in setup.py:

setup(
    # ...
    install_requires=[
        # ... (your usual install requirements)
    ],
    extras_require={
        'testing': [
            # ... (your test requirements)
        ]
    },
)

Then install your package with test requirements like this:

pip install -e ".[testing]"
Tiberiu Ichim
  • 641
  • 7
  • 7
  • @Flimm It does not. But you could pull the test packages into a list and reference the same list from `extras_require['test']` and `tests_require` like this answer does. https://stackoverflow.com/a/41398850/149428 – Taylor D. Edmiston Mar 13 '18 at 22:55
17

I am using pip to achieve something like that. Instead of adding tests_requires or extras to my setup.py I have created a pip requirements file.

Example my dev_requirements.txt file:

pytest
webtest

Then to install it run:

$ pip install -r dev_requirements.txt
Flimm
  • 136,138
  • 45
  • 251
  • 267
schettino72
  • 2,990
  • 1
  • 28
  • 27
  • 2
    I believe `requirements-dev.txt` and `requirements-test.txt` are the more commonly used names for these files. – phoenix Feb 11 '18 at 20:48
  • @phoenix the same can be said about `requirements/test.txt` and `requirements/prod.txt` – Eugene K Oct 08 '20 at 06:50