【问题标题】:install_requires based on python versioninstall_requires 基于 python 版本
【发布时间】:2014-01-31 15:44:32
【问题描述】:

我有一个适用于 python 2 和 python 3 的模块。在 Python=3.2。

类似:

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

如何做到这一点?

【问题讨论】:

    标签: python setuptools distutils distribute install-requires


    【解决方案1】:

    使用environment markers:

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

    Setuptools 的具体用法在他们的documentation 中有详细说明。上面显示的语法需要 setuptools v36.2+ (change log)

    【讨论】:

    • 不要省略python版本号两边的引号。
    【解决方案2】:

    这已在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)
    

    【讨论】:

    • 这个解决方案很脆弱,有很多 pipwheel 包的组合。当 pip 代表您构建轮子时,计算出来的 install_requires 列表会写入轮子元数据中,然后缓存的轮子可能会在不同的 Python 版本上使用。
    • 谢谢。这曾经对我有用,然后停止了。根据您的评论,我发现这是因为我最近将发布命令更改为 bdist_wheel
    • 这种方式不会生成正确的包元数据。 避免
    猜你喜欢
    • 2011-08-30
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 2012-07-13
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多