ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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:

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?

I've found this article

pip cannot uninstall from per-user site-packages directory

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?

No, you have to set one virtualenv per user:group and give proper system permissions to the folder containing it.
There is no way to install a package system-wide for all users and then selectively uninstall it for some users. If the package was installed as --user then uninstalling without --user will do what you want.
@tripleee : I do not have permission system wide and don't want to, I just need to uninstall it per current user
Figure out which directory you don't have permissions to remove, and fix its permissions, or manually move it to the side.

T
Thomas Lotze

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

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.


Are these statements accurate for running pip inside a virtual environment?
in my case uninstalling didn't work for pip 9.0.1 package is still installed at HOME/./local
Point 2 doesn't seem to be true on my system. I have a system package jedi installed in /usr/lib64/python2.7/site-packages/. When I do, as a normal user: pip install --user jedi and then pip uninstall jedi, pip tries to uninstall /usr/lib64/python2.7/site-packages/jedi and then kicks out with Permission denied. So if --user doesn't exist for the uninstall command, how do I tell uninstall not to try to uninstall /usr/lib64/python2.7/site-packages/jedi but ~/.local/lib64/python3.6/site-packages/jedi?
Exactly the same here as @AstroFloyd
Could you possibly link that unmerged MR for pip uninstall --target?
H
Huy - Logarit

example to uninstall package 'oauth2client' on MacOS:

pip uninstall oauth2client

Not sure why it has so many upvotes as it's the wrong answer. This doesn't specify the --user flag at all, if you have packages installed as root and as user, user installed package will remain.
@misantroop Read the answers carefully. It states the same thing as the accepted answer (but with less explanation)
Y
YaOzI

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

$ 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/pip executable to list or uninstall the user site packages:

(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 conclusion is: --user is not valid inside a virtual env's pip, since a user location doesn't really make sense for a virtual environment.


Too late!, I used --user inside the virtual env, is there some way to revert that?, because pip list doesn't work....
'And it seems that the current conclusion is: --user is not valid inside a virtual env's pip, since a user location doesn't really make sense for a virtual environment.' Agreed. User installs defeat the whole purpose of a virtual environment. Unfortunately there are plenty of bad examples of people doing this out there.
t
thiras

I strongly recommend you to use virtual environments for python package installation. With virtualenv, you prevent any package conflict and total isolation from your python related userland commands.

To delete all your package installed globally follow this;

It's possible to uninstall packages installed with --user flag. This one worked for me;

pip freeze --user | xargs pip uninstall -y

For python 3;

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;

pip uninstall setuptools && pip uninstall pip

Now you have clean python environment. You can create virtualenv and install the package inside them.


nice, it uninstalls all packages I've ever installed with the --user flag! now to reinstall them in my Anaconda environment...
yeah, but to be clear, this doesn't answer the original question, but rather it is uninstalling all "user" installed packages (because pip freeze --user lists all packages installed with --user), but nonetheless it could be useful in some (limited) circumstances. (To uninstall a single package {foo} installed with --user, simply run pip uninstall {foo}, as the accepted answer nicely details.)
@michael You are right. I'm editing the answer to fit with the question. Thanks for notifying
s
sorin

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


While installing a package, there was a traceback. Uninstall never worked after that.
How do I list all files pip installed for a given package (so that I can remove them)?
There is effectively impossible because pip runs setup.py which runs external code. Until setup.py is fully removed forget about it, probably by 2022 it will be.
a
a different ben

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.

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.


stackoverflow.com/a/11250821/14420 to see recipe for bath uninstalling, e.g. ./pip2 freeze | grep -v "^-e" | xargs pip uninstall -y for everything except packages installed in editable mode
M
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:

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.

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.

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.

>>>import cv2
>>>

working!