199
votes

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?

5
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
I have found in pip newsgroup that pip people don't want this functionality, at least for now. What a pitty!Michel Samia
@ThomasVanderStichele because then the answer wouldn't be available online for future Googlers :)Mark
@Mark it would be, you just write down the answer to your own question here.Thomas Vander Stichele
@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

5 Answers

117
votes

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.

297
votes

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
10
votes

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

1
votes

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 ...

-1
votes

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