Can you set conditional dependencies for Python 2 and 3 in setuptools?

前端 未结 3 1917
醉梦人生
醉梦人生 2021-02-04 01:18

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

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 01:49

    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(
        ...  ...
        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
    

提交回复
热议问题