pip how to remove incorrectly installed package with a leading dash: "-pkgname"

Question:

After running pip freeze I noticed the following warning on top of the list:

WARNING: Could not parse requirement: -atplotlib

So I checked the installed packages using pip list, and indeed the following is considered a package:

Package         Version
--------------- -------
-atplotlib      3.0.3

I assume I probably had a typo when installing/upgrading matplotlib, which led to the aforementioned "package" being installed.

But I am not able to remove it as pip uninstall -atplotlib is read as a command and returns the following error:

No such option: -a

I found the following folders:

C:UsersnameAnaconda3Libsite-packages~atplotlib

C:UsersnameAnaconda3Libsite-packages~atplotlib-3.0.3-py3.7.egg-info

Is it safe, and sufficient, to remove them?

Asked By: CAPSLOCK

||

Answers:

EDIT: According to this link, provided by Lawrence in his answer

looking for and deleting the incorrectly named folders in your site-package directory should solve the issue.

If this is not sufficient, continue the cleaning as explained below.

Searching for the name of the broken package (without the leading dash) allowed me to find the following two folders:

C:UsersnameAnaconda3Libsite-packages~atplotlib

C:UsersnameAnaconda3Libsite-packages~atplotlib-3.0.3-py3.7.egg-info

Following Hoefling’s comment (below)

I checked the SOURCES.txt file in the egg-info directory %dir%/~atplotlib-3.0.3-py3.7.egg-info/SOURCES.txt. Went through the list of paths in this file and made sure all paths listed did not contained ~. Then I renamed the directory ~atplotlib-3.0.3-py3.7.egg-info into atplotlib-3.0.3-py3.7.egg-info (removed the tilde ~).
Finally, I ran pip uninstall atplotlib, which prompted the following:

Uninstalling atplotlib-3.0.3:
Would remove:
C:UsersnameAnaconda3Libsite-packagesatplotlib-3.0.3-py3.7.egg-info
C:UsersnameAnaconda3Libsite-packagesmatplotlib
C:UsersnameAnaconda3Libsite-packagespylab.py

Proceeding with the removal solved the issue (the warning disappeared and the package is not anymore on the package list.

Answered By: CAPSLOCK

It is safe to delete the offending folder(s) from your site-packages directory.

More info below (source):

When uninstalling a package, pip will now rename it in place to a name
that cannot be imported, and once it has confirmed that everything
will succeed (including installing new versions if it’s doing an
upgrade), only then will it delete those folders. If something fails,
it renames them back.

Previously, it would copy the entire contents to another directory,
and potentially another drive, and then copy them back if it needed.
So this change is a significant performance improvement, especially
for packages with a lot of files in them.

What you’re seeing here is that the deletion failed for some reason –
perhaps pip crashed? – and so the directories were not removed. I
thought pip ignored them completely, but perhaps something else
changed since I tested that?

The directories are safe to delete.

Answered By: Lawrence

I had to do rm -rf atplotlib after removing ~ character as beginning of the file name.

$ pip freeze | grep atplotlib
$ cd /home/user/.local/lib/python3.8/site-packages
$ mv ~ atplotlib atplotlib
$ pip freeze | grep atplotlib
atplotlib ==3.0.3
$  pip uninstall atplotlib
WARNING: Skipping atplotlib as it is not installed.
$ rm -rf atplotlib
Answered By: alper

I received the following error myself, after executing:
python -m pip install --upgrade pip --user

WARNING: Ignoring invalid distribution -ip (c:python310libsite-packages)

  1. I went to the file specification within the C:
  2. Located the -ip (it was located at the top) and I deleted it.
  3. Executed the code again

Results:

Requirement already satisfied: pip in c:usersGhostFaceappdataroamingpythonpython310site-packages (21.3.1)

Answered By: bsdvs23
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.