289

When you use pip to install a package, all the required packages will also be installed with it (dependencies). Does uninstalling that package also remove the dependent packages?

Al.G.
  • 4,327
  • 6
  • 31
  • 56
Hossein
  • 40,161
  • 57
  • 141
  • 175
  • 2
    Why can't you just try it and see? I would do exactly that to answer your question, but for some reason pip cannot get the package index over here right now. – Thomas Vander Stichele Oct 27 '11 at 16:38
  • 25
    @ThomasVanderStichele because then the answer wouldn't be available online for future Googlers :) – Mark Nov 10 '16 at 11:11
  • 2
    I have found in [pip newsgroup](https://groups.google.com/d/topic/python-virtualenv/JqIbyUDy2-E/discussion) that pip people don't want this functionality, at least for now. What a pitty! – Michel Samia Apr 30 '13 at 09:19
  • 13
    @ThomasVanderStichele: I am not the OP, but here's why this is a very legit Q&A for SO: `pip` is a) notoriously brittle and version-dependent, also b) in some customer installs I've had to work with, it was installed with administrator rights (although packages weren't), hence breaking or uninstalling it was enormous grief and you had one shot to do it right. c) internet connectivity may not be great; for security reasons corporate machines are often firewalled, so you can't assume direct connectivity, and you have to know in advance everything you will need and its version, and download it. – smci May 13 '19 at 08:00
  • as of 2022, there is still no way to do it with pip. The real solution would be to remove env folder altogether and re-run `pip install -r requirements.txt` with your updated `requirements.txt` file. That installs everything needed and nothing more. – Beki Dec 21 '22 at 23:51
  • I don't really want to implement it because it's so much process but I guess you can `pip freeze` before and after and then do some sort of `diff` to isolate only new ones and then specifically `pip uninstall -r` those ones as well as the original top level in `requirements.txt`. – NeilG Aug 28 '23 at 10:15

7 Answers7

384

You can install and use the pip-autoremove utility to remove a package plus unused dependencies.

# install pip-autoremove
pip install pip-autoremove
# remove "somepackage" plus its dependencies:
pip-autoremove somepackage -y
bwv549
  • 5,243
  • 2
  • 23
  • 20
  • 19
    Unfortunately it has no real Python3 support, yet (see https://github.com/invl/pip-autoremove/issues/18) . – asmaier Oct 06 '19 at 10:31
  • 18
    Working well for me in Python 3 as of Oct 2019. – stratagem Oct 21 '19 at 09:24
  • 7
    @loved.by.Jesus - I'm on python 3.8.3 and I tested and still see the command executed as `pip-autoremove` (dash not underscore). `pip_autoremove` gives me `command not found`. – bwv549 Jun 19 '20 at 05:38
  • @bwv549 I have to explain that I executed it via `python` (when version has to be specified). In that way, we need to adapt the name to `pip_autoremove` (dash is not allowed as package name). For example `python3.8 pip_autoremove` works but `python3.8 pip-autoremove` does not. Otherwise, as you pointed out, as simple terminal command `pip-autoremove`, it works perfectly :) —though just the default python version of the system is used. Thanks for your message. – loved.by.Jesus Jun 19 '20 at 08:39
  • In a virtualenv, the pip-autoremove blindly removes sibling dependencies listed in your requirements.txt. For example, `requests==2.22.0` was listed as a requirement but was removed as a result of being a sub-dependency of another package anyway. – mmmFood Aug 06 '20 at 18:32
  • 2
    `pip-autoremove` doesn't working for me: `The 'pyqtwebengine<5.13; python_version >= "3"' distribution was not found and is required by the application` – robertspierre Nov 26 '20 at 17:26
  • Within my activated Python 3.6 environment I had to use `pip_autoremove somepackage -y` in order to remove 'somepackage'. – the_economist Dec 03 '21 at 12:17
  • 8
    did not work on windows `ModuleNotFoundError: No module named 'pip_autoremove'` (https://github.com/invl/pip-autoremove/issues/43) – lamsal Feb 04 '22 at 17:23
  • 1
    [pip3-autoremove](https://github.com/enjoysoftware/pip3-autoremove) worked for me. I'm on Windows 10 using Python 3.8.3. Running `pip3-autoremove jupyterlab -y` deleted jupyterlab including its dependencies. – Tomoyuki Aota Apr 25 '22 at 09:54
  • 14
    `pip-autoremove` has been in a broken state for a year because its devs don't maintain it properly. In order to repair it ([source](https://github.com/invl/pip-autoremove/pull/44#issuecomment-1042448589)) you have to find the file `pip_autoremove.py` which is in your python install directory at `/Scripts/` and move it to `/Lib/site-packages`, then it will work normally. @TomoyukiAota Do NOT use `pip3-autoremove`, a project that hasn't been updated for two years and has so few stars if you don't want to mess up or compromise your system. – Atralb Jun 17 '22 at 21:11
150

No, it doesn't uninstall the dependencies packages. It only removes the specified package:

$ pip install specloud
$ pip freeze # all the packages here are dependencies of specloud package

figleaf==0.6.1
nose==1.1.2
pinocchio==0.3
specloud==0.4.5

$ pip uninstall specloud
$ pip freeze

figleaf==0.6.1
nose==1.1.2
pinocchio==0.3

As you can see those packages are dependencies from specloud and they're still there, but not the specloud package itself.

As mentioned below, you can install and use the pip-autoremove utility to remove a package plus unused dependencies.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Bengineer
  • 7,264
  • 7
  • 27
  • 28
  • 4
    What about exclusive dependency packages of `specloud` package? – Fusion Jul 08 '19 at 17:23
  • @Fusion Those packages are the specloud's dependency. – Bengineer May 04 '20 at 18:56
  • 4
    Rephrasing @Fusion 's question what if you already have another package installed already (and that package has it's own dependencies) you will not be able to differentiate which dependencies belong to which package. This approach only works if you have a clean initial (virtual) environment. – Mark Aug 25 '20 at 22:16
  • 1
    @Bengineer thank you for your reply. What if I have already removed the parent package? ... and only then realized I have unused dependencies. Do I need to reinstall the parent for ```pip-autoremove``` to delete the dependencies? – Lev Barenboim Apr 17 '22 at 12:36
15

I've successfully removed dependencies of a package using this bash line:

for dep in $(pip show somepackage | grep Requires | sed 's/Requires: //g; s/,//g') ; do pip uninstall -y $dep ; done

this worked on pip 1.5.4

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
linibou
  • 688
  • 1
  • 6
  • 10
  • 69
    Although technically this solution *does* successfully remove all the dependencies, as mentioned in the other answers, it *also* uninstalls dependencies which are **not unique to the target**, including ones installed from system packages. For example, on my system this script eventually failed because the target package had dependencies in common with pip, so pip uninstalled its own dependencies before the script could finish, and then failed. – sinisterstuf Feb 17 '16 at 10:52
  • 36
    Beware this removes **only** the next level down dependencies, but **not** the dependencies of those dependencies. – tamakisquare May 09 '17 at 17:19
4

I have found the solution even though it might be a little difficult for some to carry out.

1st step (for python3 and linux):

pip3 install pip-autoremove  

2nd step:

cd /home/usernamegoeshere/.local/bin/  

3rd step:

gedit /home/usernamegoeshere/.local/lib/python3.8/site-packages/pip_autoremove.py  

and change all pip(s) to pip3
4th step:

./pip-autoremove packagenamegoeshere  

At least, this was what worked for me ...

Robert
  • 39,162
  • 17
  • 99
  • 152
david david
  • 185
  • 2
  • 14
  • This worked for me without the edits as the pip available (Fedora 36) is `pip --version pip 21.3.1 from /usr/lib/python3.10/site-packages/pip (python 3.10)` – Jim Dec 30 '22 at 17:28
0

You may have a try for https://github.com/cls1991/pef. It will remove package with its all dependencies.

cls1991
  • 46
  • 1
  • 5
0

A simple pipx.x uninstall module_name worked for me on RHEL7 to uninstall a Python package. Yes, it removed all the dependencies as well.

ex: pip3.6 uninstall pandas

J Rashid
  • 1
  • 1
0

If you're inside a venv environment in linux, you can use: pip uninstall $(pip freeze) -y, it worked like a charm here.

It will list all dependencies installed and will iterate through the list and confirm uninstall with -y parameter

Fábio Correia
  • 595
  • 7
  • 16