313

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?

2
  • 3
    I have found in pip newsgroup that pip people don't want this functionality, at least for now. What a pitty! Apr 30, 2013 at 9:19
  • 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, 2023 at 10:15

11 Answers 11

410

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
9
  • 24
    Unfortunately it has no real Python3 support, yet (see github.com/invl/pip-autoremove/issues/18) .
    – asmaier
    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 me command not found.
    – bwv549
    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 Nov 26, 2020 at 17:26
  • 8
    did not work on windows ModuleNotFoundError: No module named 'pip_autoremove' (github.com/invl/pip-autoremove/issues/43)
    – lamsal
    Feb 4, 2022 at 17:23
  • 18
    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 file pip_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 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, 2022 at 21:11
158

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.

4
  • 4
    What about exclusive dependency packages of specloud package?
    – Fusion
    Jul 8, 2019 at 17:23
  • @Fusion Those packages are the specloud's dependency.
    – Bengineer
    May 4, 2020 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, 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? Apr 17, 2022 at 12:36
18

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

2
  • 73
    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. Feb 17, 2016 at 10:52
  • 39
    Beware this removes only the next level down dependencies, but not the dependencies of those dependencies. May 9, 2017 at 17:19
5

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
  • 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, 2022 at 17:28
1

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

6
  • 1
    pef only seems to work in virtual environments, unfortunately.
    – Bas
    Jan 4, 2018 at 10:00
  • 1
    It's just in safety considerations, you are able to hack the code, simply switch off the protection code.
    – cls1991
    Jan 4, 2018 at 11:26
  • 1
    Does it make sure that those dependencies are "dangling" i.e. unneeded by other distributions?
    – wim
    Oct 18, 2018 at 18:45
  • 1
    Yeah, it's based on reference counting.
    – cls1991
    Aug 15, 2019 at 5:49
  • 1
    hey guys I have forked and modified the code. Please find the link below. github.com/nalangekrushna/pef
    – Krissh
    Sep 13, 2019 at 4:22
1

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

1

I just found a 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
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

0

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.

2
  • This is functionally the same as Fábio Correia's answer, just broken down into more steps. 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.
    – UGN
    Oct 14, 2023 at 11:00
0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.