25

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:

4

3 に答える 3

23

Bogdan のコメントは、私が進むべき道を示すのに役立ちました。他の誰かが私の問題を抱えている場合に備えて、私がしたことを投稿すると思いました。

質問の例では、ボグダンが提案したことを正確に行いました。

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,
    ]
)

ただし、これには Travis の pip スタイルの依存関係の問題がまだありますtox(Bogdan の 2 番目のコメントを考えると、その理由はわかりません)。この問題を回避するために、以下に示すように、2 つの追加の要件ファイルを作成しました。

要件-py2.txt

dnspython>=1.10.0

要件-py3.txt

dnspython3>=1.10.0

次に、Travis については、 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

最後に、 についてtoxは、これらの複数の要件ファイルを使用するかなりハックな方法を使用する必要がありました。

tox.ini

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

[testenv:py33]
deps = -rrequirements-py3.txt
于 2013-11-01T01:58:12.360 に答える
1

主に関連しているが正確ではないものについては、私の答えhttps://stackoverflow.com/a/25078063/302521とこのスクリプトを参照してください: https://gist.github.com/pombredanne/72130ee6f202e89c13bb

于 2014-08-01T10:25:01.793 に答える