Sub topic 5
Installing and uninstalling packages with python -m pip
This chapter assumes you can create and activate a virtual environment, and that you know the difference between running a script and importing a module. By the end you will be able to install, inspect, and remove packages in the right environment.
Why python -m pip
pip is Python's package installer. It downloads packages from the Python Package Index (PyPI) and places them in the site-packages directory of one specific interpreter.
You can run pip as a bare pip command or as python -m pip. The second form runs pip as a module of the python that your shell finds first. That interpreter is the one that will import the package later, so the install and the import always match.
A bare pip is a separate executable found through PATH. It can belong to a different Python than the python you use, and then the package lands in the wrong place.
With the environment active, check which interpreter pip is tied to:
python -m pip --versionThe output ends with a path such as .../.venv/lib/python3.12/site-packages/pip. If that path is not inside your environment, stop and fix the activation before installing anything.
Installing a package
python -m pip install requestspip does the following:
- It asks the index for the newest release that works with your Python version.
- It reads that release's declared dependencies and resolves compatible versions for all of them.
- It downloads and installs everything into the environment's
site-packages.
requests therefore brings along urllib3, idna, and others. You asked for one package, and pip installs several.
Requesting specific versions
Add a version specifier to restrict which releases pip may choose. Quote the argument so the shell does not treat > or < as redirection:
python -m pip install "requests>=2.31,<3"This allows any 2.x release from 2.31 upward. The next chapter covers specifiers in detail.
Inspecting what you installed
python -m pip show requestsshow prints the version, the install location, the packages it requires, and the installed packages that require it. Check the Location line to confirm the package went into your environment.
Uninstalling
python -m pip uninstall requestspip lists the files it will remove and asks for confirmation. Add -y to skip the prompt in scripts.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `ModuleNotFoundError` right after a successful install | pip and `python` belong to different interpreters | Use `python -m pip`, and check `python -m pip --version` |
| `externally-managed-environment` error | You are installing into a system Python that the OS manages | Create and activate a virtual environment first |
| `No matching distribution found` | Typo in the name, no release for your Python version, or no network | Check the spelling and the version specifier |
| `Permission denied` | Installing into a system location | Use a virtual environment instead of `sudo` |
Exercise
You activated .venv and ran pip install requests, but python -c "import requests" fails with ModuleNotFoundError. What is the most likely cause, and what command do you run to confirm it?
Show answer
The pip executable probably belongs to a different Python than the python command, so requests was installed into another interpreter. Run python -m pip --version and compare its path with python -c "import sys; print(sys.prefix)". Then reinstall with python -m pip install requests so the install targets the active interpreter.
Exercise
After running python -m pip uninstall requests, is urllib3 still installed? Why?
Show answer
Yes. pip uninstalls only the named package and does not remove the dependencies that were installed with it.