安装需要基于python版本

2024-04-28 06:54:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个在python 2和python 3上都能工作的模块。在Python<;3.2中,我想安装一个特定的包作为依赖项。对于Python>;=3.2。

类似于:

 install_requires=[
    "threadpool >= 1.2.7 if python_version < 3.2.0",
 ],

怎么能做到?


Tags: 模块installltgtifversionthreadpoolrequires
2条回答

setuptools使用environment markers对此有支持。

install_requires=[
    'enum34;python_version<"3.4"',
    'pywin32 >= 1.0;platform_system=="Windows"'
]

official documentation中详细介绍了它的用法。基于change log是在v20.5中添加的,但是在v20.8.1之前实现是不稳定的(只有15天的间隔)。


原始答案(仍然有效,但将来可能会被弃用):

setuptools支持在extras_require参数中使用。

格式如下:

extras_require={
    ':python_version=="2.7"': ["mock"],
},

它将支持其他比较运算符。


遗憾的是,在documentation中没有提到。在寻找答案时,我发现PEP-426在谈论“环境标记”。有了这个短语,我就可以找到一个setuptools ticket带有以下注释:

I've successfully used the markers feature for selectively and declaratively requiring a dependency. See backports.unittest_mock for an example. Through the 'extras', mock will be required, but only on Python 2. When I can rely on Setuptools 17.1, I can change that dependency to python_version < "3.3".

这已经讨论过了here,似乎推荐的方法是使用sys.version_info测试setup.py中的Python版本

import sys

if sys.version_info >= (3,2):
    install_requires = ["threadpool >= 1.2.7"]
else:
    install_requires = ["threadpool >= 1.2.3"]

setup(..., install_requires=install_requires)

相关问题 更多 >