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?
10 Answers
You can install and use the pip3-autoremove utility to remove a package plus unused dependencies.
# install pip3-autoremove
pip install pip3-autoremove
# remove "somepackage" plus its dependencies:
pip-autoremove somepackage -y
-
24Unfortunately it has no real Python3 support, yet (see github.com/invl/pip-autoremove/issues/18) .– asmaierCommented Oct 6, 2019 at 10:31
-
8@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 mecommand not found
.– bwv549Commented Jun 19, 2020 at 5:38 -
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
Commented Nov 26, 2020 at 17:26 -
8did not work on windows
ModuleNotFoundError: No module named 'pip_autoremove'
(github.com/invl/pip-autoremove/issues/43)– lamsalCommented Feb 4, 2022 at 17:23 -
20
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) you have to find the filepip_autoremove.py
which is in your python install directory at<python_root>/Scripts/
and move it to<python_root>/Lib/site-packages
, then it will work normally. @TomoyukiAota Do NOT usepip3-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.– AtralbCommented Jun 17, 2022 at 21:11
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.
-
4What about exclusive dependency packages of
specloud
package?– FusionCommented Jul 8, 2019 at 17:23 -
-
4Rephrasing @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.– MarkCommented Aug 25, 2020 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? Commented Apr 17, 2022 at 12:36
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
-
74Although 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. Commented Feb 17, 2016 at 10:52
-
39Beware this removes only the next level down dependencies, but not the dependencies of those dependencies. Commented May 9, 2017 at 17:19
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 ...
-
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)
– JimCommented Dec 30, 2022 at 17:28
You may have a try for https://github.com/cls1991/pef. It will remove package with its all dependencies.
-
1
-
1It's just in safety considerations, you are able to hack the code, simply switch off the protection code.– cls1991Commented Jan 4, 2018 at 11:26
-
1Does it make sure that those dependencies are "dangling" i.e. unneeded by other distributions?– wimCommented Oct 18, 2018 at 18:45
-
1
-
1hey guys I have forked and modified the code. Please find the link below. github.com/nalangekrushna/pef– KrisshCommented Sep 13, 2019 at 4:22
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
To uninstall a package with its dependencies try this simple Python script:
# pip-uninstall-with-dependencies.py
from sys import argv
from pip._internal.commands.show import search_packages_info
from pip._internal.cli.main import main
from typing import List, Dict
if len(argv) < 2:
print(f"Usage: {argv[0]} pkg1 [pkg2 ...]")
exit(1)
def get_dependency_tree(packages: List[str], candidates: Dict[str, List[str]] = dict()) -> Dict[str, List[str]]:
for package in packages:
if not candidates.get(package):
pkg_info = next(search_packages_info([package]))
candidates[package] = pkg_info.required_by
for package in pkg_info.requires:
get_dependency_tree([package])
return candidates
candidates = get_dependency_tree(argv[1:])
# eliminate required
for package in list(candidates):
diff = set(candidates[package]).difference(list(candidates))
if diff:
print(f"Package {package} cannot be removed, it is required by {diff}")
del candidates[package]
print("Uninstalling packages and dependencies:", *candidates)
for package in candidates:
main(['uninstall', '-y', package])
How to use:
pip-uninstall-with-dependencies.py crypto
NOTE: If you are concerned about removing too many packages, just remove '-y' option in the main() call.
I just found the fastest way to do it.
pip uninstall -y $(pip freeze)
It will delete all the dependencies.
If you want to make sure you won't uninstall the one that is important to you.
You can try this one.
pip freeze > ./python_uninstall_modules.txt
pip uninstall -r python_uninstall_modules.txt -y
To delete the dependencies of a specific package in your project:
1.In a new folder(outside of your project) create a new venv:
python3 -m venv env
2.Activate your new venv and in it only install the package you are trying to uninstall the dependencies of:
pip install YOUR-SPECIFIC-PACKAGE
3.Pip freeze your packages and name them something like uninstall-dependencies.txt:
pip freeze --> uninstall-dependencies.txt
4.Copy "uninstall-dependencies.txt" list to the virtual environment you are trying to remove the specific package including all its dependencies from.
5.Navigate back to your initial project (activate its environment if not already active) and use your new list as a guide to remove everything linked to the package you are uninstalling:
pip uninstall -y -r uninstall-dependencies.txt
The "-y" command performs the uninstall without asking you to confirm the removal of every single dependency
6.Test your project if everything is the way you want it to be.
7.Don't forget to pip freeze now that you have removed the package and its dependencies:
pip freeze -r requirements.txt
This should delete the specific package and its dependencies while keeping other packages in your project. Of course, there is also a possibility you have multiple packages with the same dependencies, in which case you could reinstall your new requirements.txt to set them up again.
Maybe a bit hacky but did the trick for me.
-
This is functionally the same as Fábio Correia's answer, just broken down into more steps. Commented Oct 12, 2023 at 10:57
-
Hi Karl, thanks for your comment. I am failing to see how Fábio Correia's answer is the same as mine. Fabios answer is basically "pip uninstall -y -r requirements.txt". My answer Picks a specific module from requirements.txt that came with additional dependencies and ONLY deletes that module + the dependencies that came with it. As far as I see that was what Hossein was asking for initially? Please correct me if I am wrong.– UGNCommented Oct 14, 2023 at 11:00
In my case, the problem came from the fact that I had created a pip
alias in my ~/.zshrc
file (it could be ~/.bash_profile, if you're using bash). Since my profile had alias pip=/usr/bin/pip3
, it used this installation whether I was in or out of an environment.
You can check this using which pip
in your conda
enviroment. If it still points to /usr/bin/pip3
, then it's using your User/ directory.
As a note, after you unalias pip
, and then install a package with pip
, you may still get a message that it's using a cached version. This is okay. You can user conda list
within the environment to confirm that the package was indeed installed in the environment.
pip freeze
before and after and then do some sort ofdiff
to isolate only new ones and then specificallypip uninstall -r
those ones as well as the original top level inrequirements.txt
.