931

I have installed a python package with python setup.py install.

How do I uninstall it?

Kara
  • 6,115
  • 16
  • 50
  • 57
flybywire
  • 261,858
  • 191
  • 397
  • 503
  • 60
    The accepted answer is outdated. Use `pip uninstall `. See http://stackoverflow.com/questions/1231688/how-do-i-remove-packages-installed-with-pythons-easy-install. – Joachim W Sep 12 '14 at 08:33
  • 29
    pip uninstall is not a valid answer anymore. Here is the proof. [sri@localhost python]$ pip uninstall foo DEPRECATION: Uninstalling a distutils installed project (foo) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project. – shrewmouse Mar 13 '15 at 16:45
  • @shrewmouse that does not show anymore. –  Oct 21 '18 at 23:25
  • 3
    @J.C.Rocamonde With a package I've created, the latest version of pip shows `It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.` so this is still an issue. – Josh Jan 09 '19 at 16:02
  • @Josh at the top of your setup.py file do "from setuptools import setup" instead of "from distutils.core import setup". – bigjake Jun 21 '19 at 21:00

18 Answers18

1144

Note: Avoid using python setup.py install use pip install .

You need to remove all files manually, and also undo any other stuff that installation did manually.

If you don't know the list of all files, you can reinstall it with the --record option, and take a look at the list this produces.

To record a list of installed files, you can use:

python setup.py install --record files.txt

Once you want to uninstall you can use xargs to do the removal:

xargs rm -rf < files.txt

Or if you're running Windows, use Powershell:

Get-Content files.txt | ForEach-Object {Remove-Item $_ -Recurse -Force}

Then delete also the containing directory, e.g. /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/my_module-0.1.egg/ on macOS. It has no files, but Python will still import an empty module:

>>> import my_module
>>> my_module.__file__
None

Once deleted, Python shows:

>>> import my_module
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'my_module'
Matt
  • 27,170
  • 6
  • 80
  • 74
Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
219

For me, the following mostly works:

have pip installed, e.g.:

$ easy_install pip

Check, how is your installed package named from pip point of view:

$ pip freeze

This shall list names of all packages, you have installed (and which were detected by pip). The name can be sometime long, then use just the name of the package being shown at the and after #egg=. You can also in most cases ignore the version part (whatever follows == or -).

Then uninstall the package:

$ pip uninstall package.name.you.have.found

If it asks for confirmation about removing the package, then you are lucky guy and it will be removed.

pip shall detect all packages, which were installed by pip. It shall also detect most of the packages installed via easy_install or setup.py, but this may in some rare cases fail.

Here is real sample from my local test with package named ttr.rdstmc on MS Windows.

$ pip freeze |grep ttr
ttr.aws.s3==0.1.1dev
ttr.aws.utils.s3==0.3.0
ttr.utcutils==0.1.1dev

$ python setup.py develop
.....
.....
Finished processing dependencies for ttr.rdstmc==0.0.1dev

$ pip freeze |grep ttr
ttr.aws.s3==0.1.1dev
ttr.aws.utils.s3==0.3.0
-e hg+https://vlcinsky@bitbucket.org/vlcinsky/ttr.rdstmc@d61a9922920c508862602f7f39e496f7b99315f0#egg=ttr.rdstmc-dev
ttr.utcutils==0.1.1dev

$ pip uninstall ttr.rdstmc
Uninstalling ttr.rdstmc:
  c:\python27\lib\site-packages\ttr.rdstmc.egg-link
Proceed (y/n)? y
  Successfully uninstalled ttr.rdstmc

$ pip freeze |grep ttr
ttr.aws.s3==0.1.1dev
ttr.aws.utils.s3==0.3.0
ttr.utcutils==0.1.1dev

Edit 2015-05-20

All what is written above still applies, anyway, there are small modifications available now.

Install pip in python 2.7.9 and python 3.4

Recent python versions come with a package ensurepip allowing to install pip even when being offline:

$ python -m ensurepip --upgrade

On some systems (like Debian Jessie) this is not available (to prevent breaking system python installation).

Using grep or find

Examples above assume, you have grep installed. I had (at the time I had MS Windows on my machine) installed set of linux utilities (incl. grep). Alternatively, use native MS Windows find or simply ignore that filtering and find the name in a bit longer list of detected python packages.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
  • 1
    which packaging tool/version are you using for this to work? (`distutils`, `distribute`, etc...) – Ciro Santilli OurBigBook.com May 04 '13 at 14:21
  • 1
    I use `setuptools`. You may check https://bitbucket.org/vlcinsky/ttr.aws.utils.s3 (which is not perfect). Anyway, I am quite unhappy with use of namespace packages, sometime import are broken without visible reason. But it is another story. – Jan Vlcinsky May 14 '13 at 13:25
  • 1
    correcting myself. Even though I import `setuptools`, in fact I use `distribute`. Another lesson: when installing my own package from pypi and planning to develop another one locally sharing the same namespace, **do not use** `$ pip install `, use `$ easy_install `, otherwise you run into conflicts when importing your develop package. – Jan Vlcinsky May 19 '13 at 20:47
  • 1
    To help others: because I'm not root, I install everything in my home. Here is what I did `easy_install --user --prefix=$HOME pip`. Then I added the following in my PATH: `ln -s /home/tflutre/.local/bin/pip ~/bin`, `ln -s /home/tflutre/.local/bin/pip2.7 ~/bin` and `ln -s /home/tflutre/.local/bin/pip2 ~/bin`. – tflutre Feb 20 '14 at 08:50
  • thanks for mentioning `freeze`. now i need to find out about this `develop` command. – MikeiLL Jul 08 '14 at 18:31
  • 4
    This should be accepted answer, as is much easier and **pip** usually comes with Python anyway. It is simple to use in command prompt/terminal - `pip uninstall module` or `python -m pip uninstall module` should work. Also should be cross-platform, as is part of Python. – Edward Jan 28 '16 at 17:03
  • Agree with @Edward, and it even works on Windows since Python v2.7.10. – gaborous Oct 03 '16 at 03:06
  • Hmmm, this failed with "It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall." for Python 2.7.15+, pip 18.1, Ubuntu 18.10, [this repo](https://github.com/cirosantilli/vcdvcd/pull/2/commits/025bce0740097c1ae0ecff6a4ca5d4e1034c52af). – Ciro Santilli OurBigBook.com Jan 01 '19 at 19:08
93

The #1 answer has problems:

  • Won't work on mac.
  • If a file is installed which includes spaces or other special characters, the xargs command will fail, and delete any files/directories which matched the individual words.
  • the -r in rm -rf is unnecessary and at worst could delete things you don't want to.

Instead, for unix-like:

sudo python setup.py install --record files.txt
# inspect files.txt to make sure it looks ok. Then:
tr '\n' '\0' < files.txt | xargs -0 sudo rm -f --

And for windows:

python setup.py bdist_wininst
dist/foo-1.0.win32.exe

There are also unsolvable problems with uninstalling setup.py install which won't bother you in a typical case. For a more complete answer, see this wiki page:

https://ofswiki.org/wiki/Uninstalling_setup.py_install

Yonggoo Noh
  • 1,811
  • 3
  • 22
  • 37
Ian Kelling
  • 9,643
  • 9
  • 35
  • 39
  • 3
    Why do you just not leave a suggestion under the upvoted one? Note that, these "serious problems" are about corner cases. – László Papp Sep 03 '14 at 11:30
  • 19
    I left a comment under the upvoted one. My comment is buried under a "load more comments" link which very few people click. I also made my own answer because I had a lot more to say, ie. the link in my answer. Also, corner case would not be the first term which comes to my mind when a file has a space in it. – Ian Kelling Sep 03 '14 at 23:46
  • For what it's worth, I think this will fail on filenames that contain newlines :) Whether any package maintainers are that evil, I hope I never find out. – Jack O'Connor Nov 13 '14 at 20:14
  • @JackO'Connor, that is true, and the link with more info also points that out. – Ian Kelling Nov 20 '14 at 03:32
  • 2
    On OS X Mavericks, at least, xargs does not have the -d option. Why? Because Apple loves you. Anyway this has the same effects as above, and is safe with spaces and such. It's Bash specific, though. After running the install with --record to files.txt: `OIFS=$IFS; IFS=$'\n'; FILES=( $( cat files.txt ) ); IFS=$OIFS; sudo rm -rf "${FILES[@]}"` – Joseph Sikorski Mar 31 '15 at 19:10
  • @JosephSikorski. Thanks for the insight. I've updated my answer for a mac version. Your suggested command will fail on typical programs due to argument length limit. – Ian Kelling Apr 09 '15 at 06:51
  • 2
    @IanKelling Embedding variables inside `printf`'s format string will fail if the string happens to contain e.g. `%` or ``\`` characters. Always provide a format string: `printf '%s\0' "$line"` (also avoids echo). Though you may further avoid the loop by just using `tr '\n' '\0' < files.txt | xargs -0 rm -f` – geirha Apr 13 '15 at 15:40
  • small improvement (as you use sudo to install the files) the uninstall command should be `tr '\n' '\0' < files.txt | xargs -0 sudo rm -f --` – Cwissy Apr 01 '16 at 02:35
  • This won't take care of new folders that were created by the install. – Salami Aug 19 '16 at 16:57
  • @Salami, that is already covered in the link for the "complete answer" (and it's been covered for years now) – Ian Kelling Aug 22 '16 at 11:35
  • This won't work with files containing newlines in their name. – hakre Apr 30 '22 at 11:50
50

First record the files you have installed. You can repeat this command, even if you have previously run setup.py install:

python setup.py install --record files.txt

When you want to uninstall you can just:

sudo rm $(cat files.txt)

This works because the rm command takes a whitespace-seperated list of files to delete and your installation record is just such a list.

lofidevops
  • 15,528
  • 14
  • 79
  • 119
nathan
  • 533
  • 4
  • 2
  • Is there a way to check not only if it was my program that installed it but also if since then, no other application used this resource? For example, I installed a tool, to do so, I had to install several libraries, maybe some of them were already installed (which means I can't remove in the uninstall process) but also I can't remove anything that other applications didn't installed because my library had installed first.. – Gabrielle May 02 '18 at 15:18
25

Now python gives you the choice to install pip during the installation (I am on Windows, and at least python does so for Windows!). Considering you had chosen to install pip during installation of python (you don't actually have to choose because it is default), pip is already installed for you. Then, type in pip in command prompt, you should see a help come up. You can find necessary usage instructions there. E.g. pip list shows you the list of installed packages. You can use

pip uninstall package_name

to uninstall any package that you don't want anymore. Read more here (pip documentation).

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
  • Is this a recommended way to uninstall a package that was installed by `python setup.py install`? It seems to work, but not sure if there are any side effects because it was not installed by `pip install`. Also, what about a package installed by `python setup.py develop`? Would you still recommend using `pip uninstall` over `python setup.py develop --uninstall`? – starriet Apr 07 '23 at 10:15
11

Not exactly answering the question, but something that helps me every day:

Install your packages with

pip install .

This puts the package in $HOME/.local. Uninstall with

pip uninstall <package_name>
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
8

The lazy way: simply uninstall from the Windows installation menu (if you're using Windows), or from the rpm command, provided you first re-install it after creating a distribution package.

For example,

python setup.py bdist_wininst
dist/foo-1.0.win32.exe

("foo" being an example of course).

RedGlyph
  • 11,309
  • 6
  • 37
  • 49
  • 4
    I don't know if I like it, but you get a point for orthogal thinking. :) – Lennart Regebro Oct 11 '09 at 09:26
  • To be honest, I'm not sure either hence "the lazy way" ;-) But I thought I'd mention it was possible to create more "standard" installer. It's strange the setup.py doesn't provide a clean way to remove packages though. – RedGlyph Oct 11 '09 at 09:30
  • 1
    Uninstalls require centralized registries of installed packages and it's files, and there isn't one. Discussions are ongoing on how to improve this story and it might be solved in Python 2.7/3.2 or 2.8/3.3 or so. – Lennart Regebro Oct 11 '09 at 09:33
  • That's what I thought. Well, it's good to know it might be sorted out soon, thanks the information! – RedGlyph Oct 11 '09 at 09:39
  • 1
    @Lennart: there won't be Python 2.8 at least that's the current official statement – Piotr Owsiak Jul 25 '12 at 13:30
  • 2
    @PiotrOwsiak: Yes, but that was not the statement then. 2.8 was still a possibility at that time and remained so up until PEP 404 was approved, end of last year, I think. – Lennart Regebro Jul 25 '12 at 14:28
6

Go to your python package directory and remove your .egg file, e.g.: In python 2.5(ubuntu): /usr/lib/python2.5/site-packages/

In python 2.6(ubuntu): /usr/local/lib/python2.6/dist-packages/

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
user149513
  • 1,732
  • 2
  • 11
  • 11
  • 1
    Works, unless the install installed files outside of the package, which some do, like setuptools that installs and easy_install command. – Lennart Regebro Oct 11 '09 at 09:17
  • 3
    Normally, if a package was installed using python setup.py as specified by the OP, there would not be an egg. OTOH, if there is one because easy_install was used, the documented way to uninstall packages is to use easy_install -m before deleting the egg file; otherwise, egg shells may be left behind in the easy-install.pth file. – Ned Deily Oct 11 '09 at 17:10
4

Probably you can do this as an alternative :-

1) Get the python version -

[linux machine]# python
Python 2.4.3 (#1, Jun 18 2012, 14:38:55) 

-> The above command gives you the current python Version which is 2.4.3

2) Get the installation directory of python -

[linux machine]# whereis python
python: /usr/bin/python /usr/bin/python2.4 /usr/lib/python2.4 /usr/local/bin/python2.5 /usr/include/python2.4 /usr/share/man/man1/python.1.gz

-> From above command you can get the installation directory which is - /usr/lib/python2.4/site-packages

3) From here you can remove the packages and python egg files

[linux machine]# cd /usr/lib/python2.4/site-packages
[linux machine]# rm -rf paramiko-1.12.0-py2.4.egg paramiko-1.7.7.1-py2.4.egg paramiko-1.9.0-py2.4.egg

This worked for me.. And i was able to uninstall package which was troubling me :)

COD3R
  • 129
  • 9
3

I think you can open the setup.py, locate the package name, and then ask pip to uninstall it.

Assuming the name is available in a 'METADATA' variable:

pip uninstall $(python -c "from setup import METADATA; print METADATA['name']")
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
3

Install from local python setup.py install

Uninstall from local pip uninstall mypackage

gndps
  • 461
  • 3
  • 13
2

At {virtualenv}/lib/python2.7/site-packages/ (if not using virtualenv then {system_dir}/lib/python2.7/dist-packages/)

  • Remove the egg file (e.g. distribute-0.6.34-py2.7.egg)
  • If there is any from file easy-install.pth, remove the corresponding line (it should be a path to the source directory or of an egg file).
Bhindi
  • 1,403
  • 12
  • 16
  • Thanks! Removing the corresponding line from the easy-install.pth file was the _only_ way to remove pytorch installed via `python3 setup.py develop` command. – Greg Kramida May 21 '21 at 13:44
1

Extending on what Martin said, recording the install output and a little bash scripting does the trick quite nicely. Here's what I do...

for i in $(less install.record);
sudo rm $i;
done;

And presto. Uninstalled.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
seabass
  • 1
  • 1
1

If you still have files that are supposed to be deleted after re-installing a package, make sure the folder build is also deleted. Therefore, assuming that pkg is the package you want to delete:

rm -r $(python3 -c "import pkg; print(pkg.__path__[0] + '*' )") 
rm -rf build

Obove work out for python3 and delete the package and its *.egg-info file

Ulises Rosas-Puchuri
  • 1,900
  • 10
  • 12
0

It might be better to remove related files by using bash to read commands, like the following:

sudo python setup.py install --record files.txt
sudo bash -c "cat files.txt | xargs rm -rf"
0

I had run "python setup.py install" at some point in the past accidentally in my global environment, and had much difficulty uninstalling. These solutions didn't help. "pip uninstall " didn't work with "Can't uninstall 'splunk-appinspect'. No files were found to uninstall." "sudo pip uninstall " didn't work "Cannot uninstall requirement splunk-appinspect, not installed". I tried uninstalling pip, deleting the pip cache, searching my hard drive for the package, etc...

"pip show " eventually led me to the solution, the "Location:" was pointing to a directory, and renaming that directory caused the packaged to be removed from pip's list. I renamed the directory back, and it didn't reappear in pip's list, and now I can reinstall my package in a virtualenv.

mmacvicar
  • 761
  • 6
  • 9
0

I had run python setup.py install once in my PyCharm, it installs all the packages into my conda base environment. Later when I want to remove all these packages, pip uninstall does not work. I had to delete them from /anaconda3/lib/python3.7/site-packages manually :(

So I don't see the reason why they use setup.py instead of writing requirements.txt file. The requirement file can be used to install packages in virtual environment and won't mess with system python packages.

Yossarian42
  • 1,950
  • 17
  • 14
0

I have a develop egg link set up with python setup.py develop under a conda environment and with pip uninstall <packagename> the egg link is removed. At least in this scenario, pip uninstall is one way to do this.

Shan Dou
  • 3,088
  • 1
  • 20
  • 18