由于 idna 版本冲突,Pip 要求在 Travis 中安装失败

Pip requirements installation fails in Travis due to idna version conflict

One of my Travis build tests 开始失败并出现以下错误:

The conflict is caused by:
    The user requested idna==3.1
    requests 2.25.1 depends on idna<3 and >=2.5
To fix this you could try to:
1. loosen the range of package versions you've specified
2. remove package versions to allow pip attempt to solve the dependency conflict

但是,这在我的本地机器上运行良好。例如:

(venv) C:\Users\Asus\PycharmProjects\elastic-migrate>tox -e py38
GLOB sdist-make: C:\Users\Asus\PycharmProjects\elastic-migrate\setup.py
py38 create: C:\Users\Asus\PycharmProjects\elastic-migrate\.tox\py38
py38 installdeps: -rrequirements.txt
py38 inst: C:\Users\Asus\PycharmProjects\elastic-migrate\.tox\.tmp\package\elastic-migrate-0.1.0.dev126+g8e5eb23.zip
py38 installed: appdirs==1.4.4,atomicwrites==1.4.0,attrs==20.3.0,certifi==2020.12.5,cfgv==3.2.0,chardet==4.0.0,click==7.1.2,click-log==0.3.2,codecov==2.1.11,colorama==0.4.4,coverage==5.3.1,distlib==0.3.1,elastic-migrate @ file:///C:/Us
ers/Asus/PycharmProjects/elastic-migrate/.tox/.tmp/package/1/elastic-migrate-0.1.0.dev126%2Bg8e5eb23.zip,filelock==3.0.12,flake8==3.8.4,identify==1.5.10,idna==2.10,importlib-metadata==3.3.0,iniconfig==1.1.1,jsonschema==3.2.0,mccabe==0.
6.1,more-itertools==8.6.0,nodeenv==1.5.0,packaging==20.8,pluggy==0.13.1,pre-commit==2.9.3,py==1.10.0,pycodestyle==2.6.0,pyfakefs==4.3.3,pyflakes==2.2.0,pyparsing==2.4.7,pyrsistent==0.17.3,pytest==6.2.1,pytest-cov==2.10.1,pytest-mock==3
.4.0,PyYAML==5.3.1,requests==2.25.1,requests-mock==1.8.0,setuptools-scm==5.0.1,six==1.15.0,SQLAlchemy==1.3.22,toml==0.10.2,tox==3.20.1,urllib3==1.26.2,validator-collection==1.5.0,virtualenv==20.2.2,wcwidth==0.2.5,zipp==3.4.0
py38 run-test-pre: PYTHONHASHSEED='473'

供参考:

自从我尝试向项目添加 python 3.9 支持和 pyup has upgraded the dependencies subsequently. As I've dug about it a little, I've found that there are others facing the same issues 以来,这种情况就开始发生了。但是,我找不到令人满意的方法来解决它。更好地处理 tox 环境依赖性的推荐方法是什么?一个 requirements.txt 文件似乎不是正确的方法。

历史上,pip 没有合适的依赖解析器。因此,如果您要求它安装一个没有任何版本标志的包,您将获得该包的最新版本,即使它与您已经安装的其他包冲突。

但是,随着 pip 20.3,这种情况发生了变化,现在 pip 具有更严格的依赖解析器。如果您的任何子依赖项不兼容,Pip 现在会抱怨。

作为快速修复,您可以将 requirements.txt 中的 idna 版本固定到 2.05。作为长期解决方案,您可以采用 pip-tools 之类的工具,您可以将顶级依赖项固定在 requirements.in 文件和 运行 中 pip-compile命令生成 requirements.txt 文件。这样,顶层依赖和子依赖之间就会有明确的划分。此外,该工具将为您解决子依赖冲突。