【问题标题】:Can I use Environment Markers in tests_require in setup.py?我可以在 setup.py 的 tests_require 中使用环境标记吗?
【发布时间】:2018-01-12 17:27:31
【问题描述】:

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

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

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

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

解决方案似乎是使用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 指定环境标记吗?

【问题讨论】:

  • 这不是我的能力范围。但是,其他人可能会乐于看到您的一些失败的实验。
  • 另外,为什么使用requirements.txt 不够?
  • @larsks:它们有不同的用途。 Setup.py 是您的购物清单。 Requirements.txt 是您的装箱单。 (我记得它是“安装文件包含您的需求,而需求文件包含您的设置。”)
  • 嗯...我似乎使用 setuptools 36.2.7 得到了不同的结果,而不是默认安装的 36.2.0...仍在试验中。

标签: python setuptools


【解决方案1】:

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

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

【讨论】:

  • 只是确认这对我有用,但在从 36.2.0 升级 setuptools 之后,它拒绝了它。
  • 这对我来说不适用于 setuptools 41.2.0。问题是一个依赖,在它自己的install_requires= 中列出了'typing;python_version&gt;"3.4"'... 但运行python myproj\setup.py test 无论如何都会在python 3.7 上安装打字。不仅安装了打字,而且import typing 尝试使用安装的打字,而不是标准库打字。这会导致 AttributeError 类似于 stackoverflow.com/questions/55833509/…
猜你喜欢
  • 2021-03-11
  • 2015-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多