将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
库,但这个事件使它变得足够复杂。
答案 0 :(得分:5)
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:~$
(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
(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
库而不触及任何系统包。