'Dependencies (requirements) for setting up, testing, and installing a Python library

I'm writing a Python package to be distributed and installed via PyPi.org. There are plenty of examples out there, but I can't get my mind wrapped around the proper usage of the install_requires, setup_requires, and tests_require arguments in the call to setup().

I know install_requires is the minimum set of dependencies for the library itself. This one is easy.

  1. What is the difference (if there need be any) between setup_requires and tests_require?
  2. What needs to go into each one if I want unit tests to run in a CI environment? And should unit tests run when the library gets installed?
  3. When I set up a local virtualenv for developing and testing the library, which set of requires do I want installed?


Solution 1:[1]

setup_requires: Don't use it. It was a failed experiment of setuptools. It has been obsoleted by PEP517 now (see deprecation note here) where the build system specifies build requirements declaratively in that config section, for example:

[build-system]  # in pyproject.toml
requires = ["setuptools >= 40.6.0", "wheel"]
build-backend = "setuptools.build_meta"

tests_require: Don't use it. It was a failed experiment of distutils. It has been obsoleted by projects such as pytest and tox (see deprecation note here). Nobody runs their tests by calling python setup.py test anymore, and nobody wants their test dependencies downloaded into the project directory - they want them installed into the virtualenv instead:

[options.extras_require]  # in setup.cfg
test =
    pytest
    pytest-cov

So, to address the three points directly:

  1. Both of those are cruft, omit them.

  2. Specify your test requirements elsewhere (either in a setuptools "extras_require" or in a plain old requirements_test.txt file). Yes, tests should run against the installed code.

  3. When you set up a local virtualenv for developing and testing the library, both the local package and the test requirements should be installed, with e.g. pip install -e ".[test]"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1