Search code examples
pythonunit-testingpandaspip

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


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?


Solution

  • 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