Can a package be required only for tests, not for installation?

Question:

I’m adding functionality to an existing pip-installable project, and the project owner feels that my adding of pandas to the setup.py installation requirements is ‘too heavy’, as the project should remain slim. The functionality I’m adding does not require pandas (because the functionality is operations on top of a pandas.DataFrame object), but the unit tests I wrote for it require invoking pandas to setUp a test DataFrame to mutate with.

Is there some way to require pandas only for the unit tests? Or do I just not add it to the requirements, and raise an error to manually install pandas when that unit test is run?

Asked By: selwyth

||

Answers:

For setuptools you can use tests_require in a similar fashion to install_requires to list packages that are only required for testing.

Cf. https://setuptools.pypa.io/en/latest/userguide/keywords.html?highlight=tests_require

Answered By: languitar

Option 1:

Use an "extra" with setuptools:

# setup.py
from setuptools import setup

setup(
    name="your_app",
    ...
    install_requires=...
    extras_require={
        "dev": [
            "pytest", "pandas", "coverage",  # etc
        ]
    },
)

Now when you develop on the app, use:

pip install --editable '.[dev]'

Option 2:

Don’t advertise the development requirements in the packaging metadata at all, just keep a development requirements file in the project directory and mention in the README that developers should install those after checking out the source code

pip install -r requirements-dev.txt
Answered By: wim
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.