requirements.txt 取决于 python 版本 [英] requirements.txt depending on python version

查看:31
本文介绍了requirements.txt 取决于 python 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

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 6.0:

You can use the environment markers to achieve this in requirements.txt since pip 6.0:

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

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

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

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

另请参阅需求说明符.以及 Strings 用于对应 Python 命令的字符串版本.

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

这篇关于requirements.txt 取决于 python 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆