python - 如何使用 pip 正确处理冲突的 distutils 库?

标签 python pip

pip 版本升级到 10.0.0 后,当与安装的 distutils 包发生版本冲突时,使用 pip 安装失败:

Cannot uninstall '***'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

这可能适用于 PyYAML、pyOpenSSL、urllib3、chardet 等。

我尝试通过卸载相应的软件包来解决这个问题,例如;

python-yaml python-openssl python-urllib3 python-chardet

使用apt-get (Ubuntu),然后使用pip再次安装这些库

然而,正如预期的那样,通过 apt-get 删除也会导致删除许多依赖的附加系统包,这似乎不是一个好的做法:

The following packages will be REMOVED:
apt-xapian-index cloud-init landscape-client-ui-install oneconf python-aptdaemon python-aptdaemon.gtk3widgets python-chardet python-cupshelpers python-debian python-openssl python-pip python-requests python-ubuntu-sso-client python-urllib3 python-yaml sessioninstaller software-center ssh-import-id system-config-printer-common system-config-printer-gnome system-config-printer-udev ubuntu-desktop ubuntu-release-upgrader-gtk ubuntu-sso-client ubuntu-sso-client-qt update-manager update-notifier update-notifier-common

我也不想将 pip 降级到旧版本。

那么用 pip 处理冲突的 distutils 库的最佳实践是什么?

Ps: 我以为pip 是为了方便Python 库的管理,但是这个事件让它变得很复杂。

最佳答案

Python 的虚拟环境可能有助于处理库冲突,即使是较新版本的 pip

设置虚拟环境

Python3 内置了虚拟环境。在 Python2 的情况下,virtualenv 可用于此目的。

使用以下命令设置virtualenv

sudo pip install virtualenv
venv_path="${HOME}/py_venv"
mkdir -p "${venv_path}"
virtualenv "${venv_path}"

可以通过source命令激活

source "${venv_path}/bin/activate"
(py_venv) my_user@my_machine:~$

并且可以通过deactivate命令停用

(py_venv) my_user@my_machine:~$ deactivate
my_user@my_machine:~$

确认python和pip的路径

(py_venv) my_user@my_machine:~$ which python
/home/my_user/py_venv/bin/python
(py_venv) my_user@my_machine:~$ which pip
/home/my_user/py_venv/bin/pip

请注意,默认情况下使用 sudo 执行不会指向 virtualenv

(py_venv) my_user@my_machine:~$ sudo which python
/usr/bin/python
(py_venv) my_user@my_machine:~$ sudo which pip
/usr/local/bin/pip

使用pip安装/卸载包

(py_venv) my_user@my_machine:~$ pip install ansible

成功安装到virtualenv

(py_venv) my_user@my_machine:~$ which ansible
/home/my_user/py_venv/bin/ansible

卸载virtualenv中的冲突系统包

(py_venv) my_user@my_machine:~$ pip uninstall urllib3
Skipping urllib3 as it is not installed.

在真实环境中卸载相同的包

(py_venv) my_user@my_machine:~$ deactivate
my_user@my_machine:~$ pip uninstall urllib3
Cannot uninstall 'urllib3'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

Python 虚拟环境的帮助下可以看出,可以使用较新版本的 pip 安装和卸载 Python 无需接触任何系统包的库。

关于python - 如何使用 pip 正确处理冲突的 distutils 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49916736/

相关文章:

python - NLTK 单词与 word_tokenize

python - Django 中自定义字段的自定义排序

pip - 使用 pip 安装 Python OSC

python - Jupyter Notebook 无法识别 "import torch"

python-3.x - gcc: fatal error: limits.h: 在 macOS 上没有这样的文件或目录

python - 在 GAN 中使用经过训练的判别器来计算概率

python - 分解 Pandas 数据框的列

python - python中字符串float转int

python - Python轮子名称中的 'cp3xm'和 'cp2xm'是什么意思?

python - 根据使用的导入生成需求