Can I use environment markers in test_require in setup.py?

I am considering an open source package (MoviePy) that adapts its functionality based on what packages are installed.

For example, to resize an image, it will use functions provided by OpenCV, aka PIL / pillow, aka SciPy. If none of these are available, it will drop gracefully to not support resizing.

The function setup.py

identifies some of these optional dependencies in a parameter tests_require

, so tests can be run.

However, there is an additional layer of complexity that is not handled (yet). Some of the add-on packages are not available on all supported platforms. (I think one example is the version of OpenCV they are using is not available for Python 3.3 for Windows, but please don't get stuck if this is wrong. I'm looking for a generic solution.)

The solution seems to be to use environment markers to indicate which package versions should be installed based on which version of Python and which operating system. I can do this with a requirements.txt

. I think I can do it with Conda (with a different format - colons, not semicolons). But how do you do this in setup.py?

My experiments to date have failed.

Example. Putting environment markers after package versions with semicolons:

requires = [
    'decorator>=4.0.2,<5.0',
    'imageio>=2.1.2,<3.0',
    'tqdm>=4.11.2,<5.0',
    'numpy',
    ]

optional_reqs = [
    "scipy>=0.19.0,<1.0; python_version!='3.3'",
    "opencv-python>=3.0,<4.0; python_version!='2.7'",
    "scikit-image>=0.13.0,<1.0; python_version>='3.4'",
    "scikit-learn; python_version>='3.4'",
    "matplotlib>=2.0.0,<3.0; python_version>='3.4'",
    ]

doc_reqs = [
    'pygame>=1.9.3,<2.0', 
    'numpydoc>=0.6.0,<1.0',
    'sphinx_rtd_theme>=0.1.10b0,<1.0', 
    'Sphinx>=1.5.2,<2.0',
    ] + optional_reqs

test_reqs = [
    'pytest>=2.8.0,<3.0',
    'nose', 
    'sklearn',
    'pytest-cov',
    'coveralls',
    ] + optional_reqs

extra_reqs = {
    "optional": optional_reqs,
    "doc": doc_reqs,
    "test": test_reqs,
    }

      

Then in the call to setup the parameters:

tests_require=test_reqs,
install_requires=requires,
extras_require=extra_reqs,

      

When I build this in Travis, Python 3.6.2, PIP 9.0.1:

> python setup.py install
error in moviepy setup command: 'extras_require' requirements cannot include environment markers, in 'optional': 'scipy<1.0,>=0.19.0; python_version != "3.3"'

      

Can I specify environment markers for tests_require

in setup.py

?

+3


source to share


1 answer


Yes, you can. Add an environment marker to each matching element tests_require

, separated by semicolons, for example:



tests_require=[
    'mock; python_version < "3.3"'
]

      

+2


source







All Articles