Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Environment Markers in tests_require in setup.py?

I'm looking at an open source package (MoviePy) which adapts its functionality based on what packages are installed.

For example, to resize an image, it will use the functionality provided by OpenCV, else PIL/pillow, else SciPy. If none are available, it will gracefully fallback to not support resizing.

The setup.py function identifies some of these optional dependencies in the tests_require parameter, so the tests can be run.

However, there is an additional layer of complexity that is not (yet) handled. Some of the optional packages are not available for all of the versions of the platforms supported. (I think one example is the OpenCV version they use is not available for Python 3.3 for Windows, but please don't get hung up if that is wrong. I am looking for the general solution.)

The solution would appear to be to use environment markers, to specify which versions of which packages should be installed based on which Python version and which operating system. I can do this with a requirements.txt file. I think I can do this with Conda (with a different format - colons rather than semicolons). But how do I do this in setup.py?

My experiments to date have failed.

Example: Putting the environment markers after the package versions, with a semicolon:

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 are:

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

When I build this on 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?

like image 516
Oddthinking Avatar asked Aug 04 '17 17:08

Oddthinking


1 Answers

Yes, you can. Append the environment marker to each relevant element of tests_require, separated by a semicolon, e.g.:

tests_require=[
    'mock; python_version < "3.3"'
]
like image 185
jwodder Avatar answered Oct 12 '22 14:10

jwodder