您可以在setuptools中为Python 2和3设置条件依赖吗? [英] Can you set conditional dependencies for Python 2 and 3 in setuptools?

查看:178
本文介绍了您可以在setuptools中为Python 2和3设置条件依赖吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布同时支持Python 2和3的Python egg时,您可以指定依赖于所用版本的依赖项吗?例如,如果您将 dnspython 用于Python 2,则有一个Python 3版本,称为 dnspython3



可以编写您的 setuptools.setup()函数,使您的鸡蛋可用于两个版本如果那是唯一的障碍,即是否已经运行 2to3 以确保您的库的其余部分与这两个版本兼容。



我浏览了这些文档,似乎找不到这个问题的答案:




解决方案

博格丹的评论帮助我指明了前进的道路。我想我会发布我会做的事情,以防其他人遇到我的问题。



对于问题中的示例,我确实做了Bogdan的建议:



setup.py



 导入系统

如果是sys .version_info [0] == 2:
dnspython = dnspython
elif sys.version_info [0] == 3:
dnspython = dnspython3

setup(
...< snip> ...
install_requires = [
%s> = 1.10.0%dnspython,
]

但是,这仍然存在Travis和的点子式依赖的问题毒素(鉴于Bogdan的第二点评论,我不确定为什么)。要变通解决此问题,我创建了两个额外的需求文件,如下所示:



requirements-py2.txt



  dnspython> = 1.10.0 



requirements-py3。 txt



  dnspython3> = 1.10.0 

然后对于Travis,我利用了一些我从龙卷风.travis.yml



.travis.yml



< pre class = lang-yaml prettyprint-override> install:
-如果[[$ TRAVIS_PYTHON_VERSION == 2 *]];然后pip install -r requirements-py2.txt --use-mirrors; fi
-如果[[$ TRAVIS_PYTHON_VERSION == 3 *]];然后pip install -r requirements-py3.txt --use-mirrors; fi

最后,对于毒物来使用使用这些多个需求文件的简单方法。



tox.ini



  [testenv:py27] 
deps = -rrequirements-py2.txt

[testenv:py33]
deps = -rrequirements-py3.txt


When releasing a Python egg with support for both Python 2 and 3, can you specify dependencies that change depending on which version you're using? For example, if you use dnspython for Python 2, there is a Python 3 version that is called dnspython3.

Can you write your setuptools.setup() function in such a way that your egg is useable to both versions if that is the only roadblock, i.e., if you have run 2to3 to ensure that the rest of your library is compatible with both versions.

I have looked through these documents and can't seem to find the answer this question:

解决方案

Bogdan's comment helped point me on my way. I thought I'd post what I did in case anyone else has my problem.

For the example in the question, I did exactly what Bogdan suggested:

setup.py

import sys

if sys.version_info[0] == 2:
    dnspython = "dnspython"
elif sys.version_info[0] == 3:
    dnspython = "dnspython3"

setup(
    ... <snip> ...
    install_requires=[
        "%s >= 1.10.0" % dnspython,
    ]
)

However, this still has the issue of pip-style dependencies for Travis and tox (I'm not sure why, given Bogdan's second comment). To work around this problem, I created two extra requirements files, as shown below:

requirements-py2.txt

dnspython>=1.10.0

requirements-py3.txt

dnspython3>=1.10.0

Then for Travis, I made use of some environment variables that I gleaned from the tornado .travis.yml:

.travis.yml

install:
  - if [[ $TRAVIS_PYTHON_VERSION == 2* ]]; then pip install -r requirements-py2.txt --use-mirrors; fi
  - if [[ $TRAVIS_PYTHON_VERSION == 3* ]]; then pip install -r requirements-py3.txt --use-mirrors; fi

Lastly, for tox, I had to use a rather hackish method of using these multiple requirements files.

tox.ini

[testenv:py27]
deps = -rrequirements-py2.txt

[testenv:py33]
deps = -rrequirements-py3.txt

这篇关于您可以在setuptools中为Python 2和3设置条件依赖吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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