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.

其他回答

在MacOS上卸载“oauth2client”包的示例:

pip uninstall oauth2client

但是,对于那些在虚拟环境中使用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中是无效的,因为用户位置对于虚拟环境并没有真正意义。

答案是还不可能。你必须手动移除它。

在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.

我正在运行Anaconda 4.3.22版本和python3.6.1环境,遇到了这个问题。以下是历史和解决方案:

pip uninstall opencv-python # -- the original step. failed.

ImportError: DLL load failed: The specified module could not be found.

我在python3.6环境中这样做了,得到了这个错误。

python -m pip install opencv-python # same package as above.
conda install -c conda-forge opencv # separate install parallel to opencv
pip-install opencv-contrib-python # suggested by another user here. doesn't resolve it.

接下来,我尝试下载python3.6,并将python3.dll放在该文件夹和其他文件夹中。没有什么改变。

最后,这就解决了问题:

pip uninstall opencv-python

(另一个conda-forge版本仍然安装)这只剩下conda版本,在3.6中工作。

>>>import cv2
>>>

工作!