Python 如何卸载使用 pip install --user 安装的软件包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33412974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:19:40  来源:igfitidea点击:

How to uninstall a package installed with pip install --user

pythonpython-3.xpipvirtualenv

提问by Serjik

There is a --useroption for pip which can install a Python package per user:

--userpip有一个选项可以为每个用户安装一个 Python 包:

pip install --user [python-package-name]

I used this option to install a package on a server for which I do not have root access. What I need now is to uninstall the installed package on the current user. I tried to execute this command:

我使用此选项在我没有 root 访问权限的服务器上安装软件包。我现在需要的是卸载当前用户安装的包。我试图执行这个命令:

pip uninstall --user [python-package-name]

But I got:

但我得到了:

no such option: --user

How can I uninstall a package that I installed with pip install --user, other than manually finding and deleting the package?

pip install --user除了手动查找和删除软件包之外,如何卸载我安装的软件包?

I've found this article

我找到了这篇文章

pip cannot uninstall from per-user site-packages directory

pip 无法从每个用户的站点包目录中卸载

which describes that uninstalling packages from user directory does not supported. According to the article if it was implemented correctly then with

它描述了不支持从用户目录卸载软件包。根据文章,如果正确实施,则使用

pip uninstall [package-name]

the package that was installed will be also searched in user directories. But a problem still remains for me. What if the same package was installed both system-wide and per-user? What if someone needs to target a specific user directory?

安装的软件包也将在用户目录中搜索。但对我来说仍然存在一个问题。如果在系统范围和每个用户都安装了相同的软件包怎么办?如果有人需要定位特定的用户目录怎么办?

采纳答案by Thomas Lotze

Having tested this using Python 3.5 and pip 7.1.2 on Linux, the situation appears to be this:

在 Linux 上使用 Python 3.5 和 pip 7.1.2 对此进行了测试,情况似乎是这样的:

  • pip install --user somepackageinstalls to $HOME/.local, and uninstalling it does work using pip uninstall somepackage.

  • This is true whether or not somepackageis 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 rootprivileges.

  • In addition to the predefined user install directory, pip install --target somedir somepackagewill 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 pipwill ever uninstall from are system-wide and predefined user-local, you need to run pip uninstallas the respective user to uninstall from a given user's local install directory.

  • pip install --user somepackage安装到$HOME/.local,卸载它确实可以使用pip uninstall somepackage.

  • 无论是否somepackage同时在系统范围内安装都是如此。

  • 如果在两个地方都安装了该软件包,则只​​会卸载本地一个。要使用 卸载系统范围的软件包pip,首先在本地卸载它,然后再次运行相同的卸载命令,具有root特权。

  • 除了预定义的用户安装目录外,pip install --target somedir somepackage还会将软件包安装到somedir. 无法使用pip. (但是在 Github 上有一个有点旧的未合并的 pull request 实现了pip uninstall --target。)

  • 由于唯一的pip卸载位置是系统范围和预定义的用户本地,因此您需要以pip uninstall相应用户身份运行才能从给定用户的本地安装目录卸载。

回答by sorin

The answer is Not possible yet. You have to remove it manually.

答案是不可能。您必须手动删除它。

回答by Huy - Logarit

example to uninstall package 'oauth2client' on MacOS:

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

pip uninstall oauth2client

回答by AnthonyC

As @thomas-lotze has mentioned, currently pip tooling does not do that as there is no corresponding --user option. But what I find is that I can check in ~/.local/bin and look for the specific pip#.# which looks to me like it corresponds to the --user option.

正如@thomas-lotze 所提到的,目前 pip 工具不会这样做,因为没有相应的 --user 选项。但是我发现我可以检查 ~/.local/bin 并查找特定的 pip#.# ,在我看来它对应于 --user 选项。

In my case:

就我而言:

antho@noctil: ~/.l/bin$ pwd
/home/antho/.local/bin
antho@noctil: ~/.l/bin$ ls pip*
pip  pip2  pip2.7  pip3  pip3.5

And then just uninstall with the specific pip version.

然后只需使用特定的 pip 版本卸载。

回答by Marc Maxmeister

I am running Anaconda version 4.3.22 and a python3.6.1 environment, and had this problem. Here's the history and the fix:

我正在运行 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.

I did this into my python3.6 environment and got this error.

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

Next, I tried downloading python3.6 and putting the python3.dll in the folder and in various folders. nothing changed.

接下来,我尝试下载python3.6并将python3.dll放在文件夹和各种文件夹中。没有改变。

finally, this fixed it:

最后,这修复了它:

pip uninstall opencv-python

(the other conda-forge version is still installed) This left only the conda version, and that works in 3.6.

(仍然安装了另一个 conda-forge 版本)只剩下 conda 版本,并且可以在 3.6 中使用。

>>>import cv2
>>>

working!

在职的!

回答by YaOzI

Be careful though, for those who using pip install --user some_pkginside a virtual environment.

不过要小心,对于那些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

In this case, you have to deactivate the current virtual environment, then use the corresponding python/pipexecutable to list or uninstall the user site packages:

在这种情况下,您必须停用当前的虚拟环境,然后使用相应的python/pip可执行文件列出或卸载用户站点包:

(my_py_venv) $ deactivate
$ path/to/python -m pip list
$ path/to/python -m pip uninstall some_pkg

Note that this issue was reported few years ago. And it seems that the current conclusionis: --useris not valid inside a virtual env's pip, since a user location doesn't really make sense for a virtual environment.

请注意,这个问题是几年前报道的目前的结论似乎是:在虚拟环境中--user无效pip,因为用户位置对于虚拟环境没有真正意义。

回答by thiras

I think it's possible to uninstall packages installed with --userflag. This one worked for me;

我认为可以卸载使用--userflag安装的软件包。这个对我有用;

pip freeze --user | xargs pip uninstall -y

pip freeze --user | xargs pip uninstall -y

For python 3;

对于python 3;

pip3 freeze --user | xargs pip3 uninstall -y

pip3 freeze --user | xargs pip3 uninstall -y

But somehow these commands don't uninstall setuptools and pip. After those commands (if you really want clean python) you may delete them with;

但不知何故,这些命令不会卸载 setuptools 和 pip。在这些命令之后(如果你真的想要干净的python)你可以删除它们;

pip uninstall setuptools && pip uninstall pip

pip uninstall setuptools && pip uninstall pip