requirements.txt 取决于 python 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19559247/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:03:13  来源:igfitidea点击:

requirements.txt depending on python version

pythonpip

提问by aquavitae

I'm trying to port a python2 package to python3 (not my own) using six so that it's compatible with both. However one of the packages listed in requirements.txt is now included in the python3 stdlib and the pypi version doesn't work in python3 so I want to conditionally exclude it. Doing this in setup.py is easy, I can just do something like:

我正在尝试使用六个将 python2 包移植到 python3(不是我自己的),以便它与两者兼容。但是,requirements.txt 中列出的包之一现在包含在 python3 stdlib 中,而 pypi 版本在 python3 中不起作用,所以我想有条件地排除它。在 setup.py 中执行此操作很容易,我可以执行以下操作:

if sys.version_info[0] == 2:
    requirements += py2_requirements
else:
    requirements += py3_requirements

But I would like requirements.txt to reflect the correct list too. I can't find anything on this in the pip documentation. so does anyone know how to do it, or if it is even possible?

但我希望 requirements.txt 也能反映正确的列表。我在 pip 文档中找不到任何关于此的内容。那么有谁知道该怎么做,或者甚至可能吗?

采纳答案by Jiangge Zhang

You can use the environment markersto achieve this in requirements.txtsince pip 6.0:

您可以使用环境标记来实现这一点,requirements.txt因为pip 6.0

SomeProject==5.4; python_version < '2.7'
SomeProject; sys_platform == 'win32'

It is supported by setuptools too by declaring extra requirements in setup.py:

setuptools 也通过在 中声明额外要求来支持它setup.py

setup(
    ...
    install_requires=[
        'six',
        'humanize',
    ],
    extras_require={
        ':python_version == "2.7"': [
            'ipaddress',
        ],
    },
)

See also requirement specifiers. And Stringsfor the string versions of corresponding Python commands.

另见需求说明符。和字符串对应的Python命令的字符串版本。

回答by Leonardo.Z

You can create multiple requirements files, put those common packages in a common file, and include them in another pip requirements file with -r file_path

您可以创建多个需求文件,将这些公共包放在一个公共文件中,然后将它们包含在另一个 pip 需求文件中 -r file_path

requirements/
  base.txt
  python2.txt
  python3.txt

python2.txt:

python2.txt:

-r base.txt
Django==1.4 #python2 only packages

python3.txt:

python3.txt:

-r base.txt
Django==1.5 #python3 only packages

pip install -r requirements/python2.txt

pip install -r 要求/python2.txt