241

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,175
  • 6
  • 34
  • 55
Hossein
  • 37,539
  • 55
  • 135
  • 174
  • 1
    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
  • 18
    @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
  • 1
    @Mark it would be, you just write down the answer to your own question here. – Thomas Vander Stichele Jan 23 '17 at 20:08
  • 10
    @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

5 Answers5

342

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
  • 4,718
  • 2
  • 20
  • 20
  • 14
    Unfortunately it has no real Python3 support, yet (see https://github.com/invl/pip-autoremove/issues/18) . – asmaier Oct 06 '19 at 10:31
  • 14
    Working well for me in Python 3 as of Oct 2019. – stratagem Oct 21 '19 at 09:24
  • 6
    @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
  • 4
    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
139

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.

Bengineer
  • 6,752
  • 7
  • 24
  • 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
  • 3
    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
  • @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
12

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

linibou
  • 614
  • 1
  • 5
  • 10
  • 61
    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
  • 32
    Beware this removes **only** the next level down dependencies, but **not** the dependencies of those dependencies. – tamakisquare May 09 '17 at 17:19
1

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
  • 36,354
  • 15
  • 89
  • 140
david david
  • 145
  • 1
  • 13
-1

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

cls1991
  • 36
  • 5