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
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
9
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
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
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
You may have a try for https://github.com/cls1991/pef.
It will remove package with its all dependencies.
6
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.
2
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.