在 setup.py 中强制执行 python 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19534896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enforcing python version in setup.py
提问by Santhosh
Currently, we are setting\installing up some packages on system by mentioning their version and dependencies in setup.py under install_requires attribute. Our system requires python 2.7. Sometimes, users are having multiple versions of python say 2.6.x and 2.7, some packages it says are available already but actually on the system available under 2.6 site packages list. Also some users has 2.6 only, how to enforce from setup.py or is there any other way to say to have only python 2.7 and all packages which we want setup.py to update are for only 2.7. We require minimum 2.7 on the machine to run our code.
目前,我们正在通过在 install_requires 属性下的 setup.py 中提及它们的版本和依赖项来在系统上设置\安装一些软件包。我们的系统需要 python 2.7。有时,用户有多个版本的 python,比如 2.6.x 和 2.7,它说的一些包已经可用,但实际上在 2.6 站点包列表下可用的系统上。还有一些用户只有 2.6,如何从 setup.py 强制执行,或者有没有其他方法可以说只有 python 2.7 并且我们希望 setup.py 更新的所有包都只有 2.7。我们需要机器上的最低 2.7 来运行我们的代码。
Thanks! Santhosh
谢谢!桑托什
采纳答案by Ewan
As the setup.py
file is installed via pip
(and pip
itself is run by the python interpreter) it is not possible to specify which Python version to use in the setup.py
file.
由于setup.py
文件是通过pip
(并且pip
本身由 python 解释器运行)安装的,因此无法指定要在setup.py
文件中使用的 Python 版本。
Instead have a look at this answerto setup.py: restrict the allowable version of the python interpreterwhich has a basic workaround to stop the install.
相反,看看这个答案给setup.py:限制Python解释器的版本允许其有一个基本的解决方法,以停止安装。
In your case the code would be:
在您的情况下,代码将是:
import sys
if sys.version_info < (2,7):
sys.exit('Sorry, Python < 2.7 is not supported')
回答by Aaron V
The current best practice (as of this writing in March 2018) is to add a python_requires
argument directly to the setup()
call in setup.py
:
当前的最佳实践(截至 2018 年 3 月撰写本文时)是python_requires
直接在setup()
调用中添加参数setup.py
:
from setuptools import setup
[...]
setup(name="my_package_name",
python_requires='>3.5.2',
[...]
Note that this requires setuptools>=24.2.0 and pip>=9.0.0; see the documentationfor more information.
请注意,这需要 setuptools>=24.2.0 和 pip>=9.0.0;有关更多信息,请参阅文档。