Solve – pip how to remove incorrectly installed package with a leading dash: “-pkgname”

Solve – pip how to remove incorrectly installed package with a leading dash: “-pkgname” is an error that occurs when there is an offending folder.

In this article I am going to detail how I can solve this error and why the error is even there in the first place, we will also explore other solutions that may work for you in case the first solution does not work.

Describing : pip how to remove incorrectly installed package with a leading dash: “-pkgname”

Let us try and replicate this error, step by step.

If you make a typo for example and then run the code bellow

                                                                       #
pip freeze
                                                                       #

You are going to end up with the following warning

                                                                       #
WARNING: Could not parse requirement: -lotly
                                                                       #

Let us check the packages that we have already installed, to do that we can run the following code

                                                                       #
pip list
                                                                       #

This is the result we got

                                                                       #
Package         Version
--------------- -------
-lotly           3.0.3
                                                                       #

The issue starts when we have the typo after upgrading a package called plotly, now we are stuck with this error.

When we try removing the faulty package using the following code

                                                                       #
pip uninstall -lotly
                                                                       #

We get the following error instead

                                                                       #
No such option: -a
                                                                       #

We can see that now I have the following folders

                                                                       #
C:\Users\name\Anaconda3\Lib\site-packages\~lotly

C:\Users\name\Anaconda3\Lib\site-packages\~lotly-3.0.3-py3.7.egg-info
                                                                       #

Now I need to remove the faulty package, I assume you have the same problem, that is why you are here and I prepared some solution for you.

Solution 1 : delete the incorrectly named folders in your site-package directory

The solution is simple or a little bit more complex depending on your particular case

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

The first attempt should be trying to delete the incorrectly named folders in the site-package directory.

This should be enough to solve the issue.

In this case navigate to the Path C:\Anaconda3\Lib\site-packages and delete the faulty folder.

Solution 2 : Editing SOURCES.txt

If the solution above did not work I have another more powerful solution for you.

The solution is to edit sources.txt, you can find the folder by following this path: %dir%/~lotly-3.0.3-py3.7.egg-info/SOURCES.txt

Bu default the file contains a list of paths, make sure to go through the list of all paths and edit them by removing every occurrence of the character ~

Then rename the directory causing the error by removing the character ~

So this directory will be renamed

                                                                       #
C:\Users\name\Anaconda3\Lib\site-packages\~lotly-3.0.3-py3.7.egg-info
                                                                       #

To the following

                                                                       #
C:\Users\name\Anaconda3\Lib\site-packages\lotly-3.0.3-py3.7.egg-info
                                                                       #

We just removed the character ~

Now run the following code

                                                                       #
pip uninstall lotly
                                                                       #

That is it guys, this is the solution, the package should be removed and gone forever

If you found this helpful, please consider helping us by donating to our Kofi account using the red button above

Solution 3 : use rm -rf lotly after removing the character ~

Another solution that worked for other people, which is very similar to ours is to run

                                                                       #
rm -rf lotly
                                                                       #

After removing the character ~ from the beginning of the filename.

This should solve the issue for most people.

Summing-up

Finally, we are at the end of this article, I hope this article has been helpful, I hope you solved your problem, coding can be hard when you have a lot of confusing errors here and there.

Thank you for reading, keep learning and keep coding, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/