首页 > 解决方案 > 为什么 pip install 有时会选择不兼容的版本?

问题描述

在我的 python 包 htrest (https://pypi.org/project/htrest/)中,我有以下要求:

requirements = [
    'htheatpump==1.2.1',
    'Flask==1.1.1',
    'flask-restx==0.1.1',
    'Flask-BasicAuth==0.2.0',
    # put package requirements here
]

当我使用它安装它时pip install htrest,有时会失败并显示以下消息:

flask-restx 0.1.1 has requirement werkzeug<=0.16.1, but you'll have werkzeug 1.0.0 which is incompatible.

看起来 pip 是因为( )werkzeug==1.0.0的要求而选择的FlaskWerkzeug>=0.15

Collecting Werkzeug>=0.15 (from Flask==1.1.1->htrest)
  Using cached https://files.pythonhosted.org/packages/ba/a5/d6f8a6e71f15364d35678a4ec8a0186f980b3bd2545f40ad51dd26a87fb1/Werkzeug-1.0.0-py2.py3-none-any.whl

虽然flask-restx需要werkzeug<=0.16.1.

另一方面,有时 pip 会选择合适的werkzeug( 0.16.1) 版本:

Collecting werkzeug<=0.16.1 (from flask-restx==0.1.1->htrest)
  Using cached https://files.pythonhosted.org/packages/c2/e4/a859d2fe516f466642fa5c6054fd9646271f9da26b0cac0d2f37fc858c8f/Werkzeug-0.16.1-py2.py3-none-any.whl

来满足 和 的Flask 要求 flask-restx

谁能解释一下原因以及如何解决这个问题?

问候,丹尼尔。

标签: pythonpip

解决方案


默认情况下,该pip install <package_name>命令总是查找最新版本的软件包并安装它。同时,它还会搜索包元数据中列出的最新版本的依赖项并安装这些依赖项,以确保包具有所需的所有要求。

如果要安装以前的版本,则必须指定此版本。本文提供了有关 pip 工作原理的完整详细信息

在您的情况下,您应该这样进行:

pip3 uninstall Werkzeug        # uninstalling the current Werkzeug
pip3 install Werkzeug==0.16.1  # install specific version of Werkzeug

推荐阅读