pip有一个——user选项,可以为每个用户安装Python包:
pip install --user [python-package-name]
我使用此选项在我没有根访问权限的服务器上安装包。我现在需要的是卸载当前用户上安装的包。我试图执行这个命令:
pip uninstall --user [python-package-name]
但我得到了:
no such option: --user
如何卸载我用pip install——user安装的包,而不是手动查找和删除包?
我找到了这篇文章
PIP无法从每个用户的site-packages目录卸载
说明不支持从用户目录卸载包。根据这篇文章,如果它被正确执行,那么
pip uninstall [package-name]
还将在用户目录中搜索已安装的包。但我仍然有一个问题。如果在系统范围内和每个用户都安装了相同的包呢?
如果有人需要针对特定的用户目录怎么办?
在Linux上使用Python 3.5和pip 7.1.2进行测试后,情况似乎是这样的:
pip install --user somepackage installs to $HOME/.local, and uninstalling it does work using pip uninstall somepackage.
This is true whether or not somepackage is also installed system-wide at the same time.
If the package is installed at both places, only the local one will be uninstalled. To uninstall the package system-wide using pip, first uninstall it locally, then run the same uninstall command again, with root privileges.
In addition to the predefined user install directory, pip install --target somedir somepackage will install the package into somedir. There is no way to uninstall a package from such a place using pip. (But there is a somewhat old unmerged pull request on Github that implements pip uninstall --target.)
Since the only places pip will ever uninstall from are system-wide and predefined user-local, you need to run pip uninstall as the respective user to uninstall from a given user's local install directory.
但是,对于那些在虚拟环境中使用pip install——user some_pkg的人要小心。
$ path/to/python -m venv ~/my_py_venv
$ source ~/my_py_venv/bin/activate
(my_py_venv) $ pip install --user some_pkg
(my_py_venv) $ pip uninstall some_pkg
WARNING: Skipping some_pkg as it is not installed.
(my_py_venv) $ pip list
# Even `pip list` will not properly list the `some_pkg` in this case
在这种情况下,你必须停用当前的虚拟环境,然后使用相应的python/pip可执行文件来列出或卸载用户站点包:
(my_py_venv) $ deactivate
$ path/to/python -m pip list
$ path/to/python -m pip uninstall some_pkg
请注意,这个问题是几年前报道的。目前的结论似乎是:——user在虚拟环境的pip中是无效的,因为用户位置对于虚拟环境并没有真正意义。