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

时间:2018-04-19 08:52:14

标签: 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库,但这个事件使它变得足够复杂。

1 个答案:

答案 0 :(得分:5)

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

设置virtualenv

Python3内置了虚拟环境。如果是Python2virtualenv可用于此目的。

使用以下命令设置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库而不触及任何系统包。