Can a package be required only for tests, not for

2019-06-26 03:12发布

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?

2条回答
ゆ 、 Hurt°
2楼-- · 2019-06-26 03:56

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.readthedocs.io/en/latest/setuptools.html?highlight=tests_require

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-06-26 03:57

Yes, it's simple in 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]
查看更多
登录 后发表回答