Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pip install test dependencies for tox from setup.py

I made my project with setuptools and I want to test it with tox. I listed dependencies in a variable and added to setup() parameter (tests_require and extras_require). My project needs to install all of the dependencies listed in tests_require to test but pip install is not installing them.

I tried this but it did not work:

install_command = pip install {opts} {packages}[tests] 

How can I install test dependencies without having to manage multiple dependency lists (i.e. Having all dependencies listed in both test_requirements.txt and the tests_require variable)?

like image 351
item4 Avatar asked Apr 25 '15 20:04

item4


People also ask

Does pip install dependencies of dependencies?

Pip will not flag dependency conflicts. As a result, it will happily install multiple versions of a dependency into your project, which will likely result in errors.

How does pip use setup py?

If you use setup.py , you have to visit the library's website, figure out where to download it, extract the file, run setup.py ... In contrast, pip will automatically search the Python Package Index (PyPi) to see if the package exists there, and will automatically download, extract, and install the package for you.


2 Answers

I've achieved this by committing a slight abuse of extra requirements. You were almost there trying the extras syntax, just that tests_require deps aren't automatically available that way.

With a setup.py like this:

from setuptools import setup  test_deps = [     'coverage',     'pytest', ] extras = {     'test': test_deps, }  setup(     # Other metadata...     tests_require=test_deps,     extras_require=extras, ) 

You can then get the test dependencies installed with the extras syntax, e.g. from the project root directory:

$ pip install .[test] 

Give that same syntax to Tox in tox.ini, no need to adjust the default install_command:

[testenv] commands = {posargs:pytest} deps = .[test] 

Now you don't need to maintain the dependency list in two places, and they're expressed where they should be for a published package: in the packaging metadata instead of requirements.txt files.

It seems this little extras hack is not all that uncommon.

like image 146
ches Avatar answered Sep 21 '22 19:09

ches


Solution

Tox 2.6 introduced extras option. This will install extras from the sdist it just built, only for that sdist and at the time it was doing the normal sdist install.

setup.py should look like:

setuptools.setup(     ...     extras_require={         'tests': ['pytest>=3.7.0', 'more_packages'],     },     ...  ) 

tox.ini should look like:

[testenv] ... extras = tests ... 

Concerns

Other approaches may get similar results but introduce unnecessary risk and limits the usefulness of other features:

deps =.[tests] is a bit of a hack. The field is for packages the environment needs. If setup.py install_requires references another package you develop, you could use it to pull in a pre-release version of it. As shown, it will install your whole package from your working directory (whatever state that is in!) just to get at the list of packages in tests. install_command will run next, installing your newly minted sdist. In short, issues with the sdist may be masked since you already have installed from your working copy.

Editing install_command is overkill. It'll overwrite items installed via deps. (again maybe you used it to install a specific version of a package).

tests_require is used when python setup.py test is run. Tox recommends avoiding python setup.py test so you can ignore tests_require all together.

like image 44
Guy Gangemi Avatar answered Sep 21 '22 19:09

Guy Gangemi