首页 > 解决方案 > 如何让不同版本的 Python 包共存

问题描述

我已经为我的 project1 安装了 1.2.0 版本的 kafka 包,当我为 project2 安装 1.3.0 版本时,以前的版本被覆盖,然后 project1 将无法运行,我该怎么做才能保持两个项目正常运行?

D:\soar\totems-siip-soar-plugins\totems-siip-soar-plugins-pycommon>pip show kafka
Name: kafka
Version: 1.2.0
Summary: Pure Python client for Apache Kafka
Home-page: https://github.com/dpkp/kafka-python
Author: Dana Powers
Author-email: dana.powers@gmail.com
License: Apache License 2.0
Location: c:\users\administrator\appdata\local\programs\python\python39\lib\site-packages
Requires: six
Required-by: totems-pycommon

当我安装其他版本时:

D:\soar\totems-siip-soar-plugins\totems-siip-soar-plugins-pycommon>pip install kafka==1.3.0
Looking in indexes: http://192.168.218.125:8081/repository/pypi_group_test/simple
Collecting kafka==1.3.0
  Downloading http://192.168.218.125:8081/repository/pypi_group_test/packages/kafka/1.3.0/kafka-1.3.0-py2.py3-none-any.whl (193 kB)
     |████████████████████████████████| 193 kB ...
Installing collected packages: kafka
  Attempting uninstall: kafka
    Found existing installation: kafka 1.2.0
    Uninstalling kafka-1.2.0:
      Successfully uninstalled kafka-1.2.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
totems-pycommon 1.0.0 requires kafka==1.2.0, but you have kafka 1.3.0 which is incompatible.
Successfully installed kafka-1.3.0

标签: pythoninstallationpipversion

解决方案


您可以使用 python 的venv创建具有独立包的单独环境

要创建环境,您可以使用以下命令:

python3 -m venv /some/path/env-name

之后,您可以使用

/some/path/env-name/Scripts.bat            # on Windows
source /some/path/env-name/bin/activate    # on Linux

您在环境处于活动状态时安装的软件包仅在该环境中可用。通过为每个项目设置环境,您可以避免您正在谈论的那种冲突。


推荐阅读