我可以在 setup.py 中使用 tests_require 中的环境标记吗?

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

我正在查看一个开源包 (MoviePy),它根据安装的包调整其功能。

例如,要调整图像大小,它将使用 OpenCV 提供的功能,else PIL/pillow,else SciPy。如果 none 可用,它将优雅地回退到不支持调整大小。

setup.py 函数在 tests_require 参数中标识了其中一些可选依赖项,因此测试可以是 运行.

但是,还有一层(尚未)处理的复杂性。某些可选包并非适用于所有受支持平台的版本。 (我认为一个例子是他们使用的 OpenCV 版本不适用于 Python 3.3 for Windows,但如果那是错误的,请不要挂断电话。我正在寻找通用解决方案。)

解决方案似乎是使用 environment markers,根据哪个 Python 版本和哪个操作系统指定应安装哪些软件包的哪些版本。我可以用 requirements.txt 文件来做到这一点。我想我可以用 Conda 做到这一点(使用不同的格式 - 冒号而不是分号)。但是我如何在 setup.py 中执行此操作?

我迄今为止的实验都失败了。

示例:将环境标记放在包版本之后,带分号:

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,
    }

然后在调用setup时,参数为:

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

当我在 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"'

我可以在 setup.py 中为 tests_require 指定环境标记吗?

是的,你可以。将环境标记附加到 tests_require 的每个相关元素,以分号分隔,例如:

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