3

If I have a setup.py using SetupTools, is there a way to specify install_requires for a specific version of Python?

E.g., if one of my modules requires the use of an OrderedDict, I would like versions of Python <2.7 to install the ordereddict1.1 package from PyPI, but there's no reason to (and probably a bad idea) to add that to a Python 2.7 installtion.

What's the best way to handle this? Separate eggs for the different versions? I know that's necessary for non-pure modules but this would be pure Python.

4

2 回答 2

4

setup.py只是简单的 Python 代码,因此您在设置脚本的源代码中执行完全相同的操作。


该文档显示了如何打开sys.version_info3.x 与 2.x 代码,但它对 2.7 与 2.6 的工作方式相同。因此,如果您的代码正在执行此操作:

if sys.version_info < (2, 7);
    from ordereddict import OrderedDict
else:
    from collections import OrderedDict

…然后你的安装脚本可以做到这一点:

import sys
from setuptools import setup

extra_install_requires = []
if sys.version_info < (2, 7):
    extra_install_requires.append('ordereddict>=1.1')

setup(
    # ...
    install_requires = [...] + extra_install_requires,
    # ...
)

另一方面,如果您的代码正在执行此操作:

try:
    from collections import OrderedDict
except ImportError:
    from ordereddict import OrderedDict

…那么,虽然你可以使用version_info,但你不妨这样做:

extra_install_requires = []
try:
    from collections import OrderedDict
except ImportError:
    extra_install_requires.append('ordereddict>=1.1')

无论哪种方式,如果你,例如,pip-2.5 install这个包,它会下载并安装ordereddict模块(除非用户已经有 1.1 或更高版本);2.7,它不会做任何事情。


如果您希望分发预先构建的鸡蛋,那么是的,它们最终会在 Python 2.6 和 2.7 中有所不同。例如,在 之后python2.6 setup.py bdist_egg && python2.7 setup.py bdist_egg,您将得到dist/Foo-0.1-py2.6.eggand dist/Foo-0.1-py2.7.egg,并且您必须同时分发它们。

于 2013-10-01T21:14:07.597 回答
0

您可以只检查 python 版本并动态附加旧 python 版本的要求:

from setuptools import setup
import sys

install_requires = [
   # your global requirements
   # ...
]

if sys.version_info < (2, 7):
    install_requires.append('ordereddict >= 1.1')

setup(
    # ...
    install_requires=install_requires
    # ...
)
于 2013-10-01T21:13:20.283 回答