Does uninstalling a package with pip also remove the dependent packages?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When you remove a package with pip uninstall, pip only removes the package you name. It does not automatically uninstall the dependencies that were installed alongside it, because pip cannot safely assume those packages are no longer needed by something else in the same environment.
What pip uninstall Actually Removes
Suppose you install a package that depends on several others:
If you later run:
requests is removed, but packages such as urllib3, idna, charset-normalizer, and certifi usually remain installed. That behavior is intentional.
The reason is simple: a dependency may have been installed for more than one top-level package. If pip aggressively removed it, uninstalling one package could break another package that still depends on it.
You can verify what is still installed:
This often surprises developers who expect package managers to perform automatic dependency cleanup. Some ecosystem tools do that, but plain pip does not.
Why pip Behaves Conservatively
pip focuses on installing and uninstalling distributions, not on maintaining a full dependency ownership graph the way some other package managers do. In a shared environment, it is difficult to know whether a dependency is now unused or whether another project or direct install still relies on it.
For example, imagine this sequence:
Now remove requests. A leftover package might still be required by boto3 or another dependency chain. Keeping the dependency is safer than guessing wrong.
That conservative model is one reason Python development works best with isolated virtual environments. Instead of trying to prune a long-lived global environment perfectly, create a fresh environment per project and delete the whole environment when the project is no longer needed.
Safer Ways to Clean Up Dependencies
The simplest cleanup strategy is to recreate the environment from a requirements file rather than trying to hand-prune it forever.
If the environment becomes messy, remove it and build it again:
That is usually more reliable than guessing which transitive packages are safe to remove.
If you want to inspect dependency relationships, pipdeptree is useful:
It shows which packages depend on which others, so you can see whether a leftover package is still referenced.
There are also third-party tools such as pip-autoremove that try to remove unneeded dependencies:
Use these tools carefully. They are convenient, but they still rely on the current environment state and can remove something you expected to keep if your environment is already inconsistent.
Prefer Virtual Environments Over Global Cleanup
If you install packages globally and reuse that interpreter for many unrelated tasks, dependency cleanup becomes messy fast. A virtual environment gives you a disposable sandbox.
For day-to-day work, the best workflow is:
- create one environment per project
- record direct dependencies in
requirements.txtorpyproject.toml - rebuild the environment when it drifts too far from the declared state
That approach avoids most uninstall confusion because you do not need the environment to live forever.
Common Pitfalls
The most common mistake is assuming pip uninstall some-package restores the environment to the exact state from before installation. It does not. Dependencies usually remain.
Another issue is deleting packages from a shared interpreter and then breaking a different project. This happens often when developers work outside a virtual environment.
Some teams also rely on pip list alone and treat every installed package as a direct dependency. In reality, many entries are transitive, so removing them manually without checking the dependency tree can destabilize the environment.
Finally, be cautious with third-party autoremove tools. They can be helpful, but they are not a substitute for environment isolation and reproducible dependency files.
Summary
- '
pip uninstallremoves the named package, not its dependencies.' - Leftover dependencies stay installed because
pipcannot assume they are unused. - Use
pipdeptreeif you need to inspect dependency relationships. - Third-party cleanup tools exist, but they should be used carefully.
- The most reliable cleanup strategy is one virtual environment per project.
Related reading
- Double Iteration in List Comprehension
- Downgrade Python version in virtual environment
- Download a folder from S3 using Boto3
- Download file from AWS S3 using Python
- Download file from web in Python 3
- Download large file in python with requests
- Downloading a picture via urllib and python
- Drop all duplicate rows across multiple columns in Python Pandas
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.