Sub topic 14
Installing packages with pip
This chapter assumes you can open a terminal, have Python 3 installed, and know how to create and activate a virtual environment with venv. If your prompt does not show (.venv) after activation, revisit the virtual environments chapter first.
What pip does
pip is Python's package installer. A package is code written by someone else that you can use in your own programs. pip downloads packages from the Python Package Index (PyPI), a public catalog, and places them in the environment of the interpreter that runs pip.
The key point for avoiding errors is that pip belongs to one specific Python. If you install a package with one interpreter and run your script with another, Python will not find it.
Install with `python -m pip`
Run pip as a module of the interpreter you intend to use. This guarantees the package lands in the right place.
python3 -m venv .venv
source .venv/bin/activate
# On Windows (Command Prompt): .venv\Scripts\activate.bat
# On Windows (PowerShell): .venv\Scripts\Activate.ps1
python -m pip install requests
python -m pip listInside an activated environment, python points to the environment's interpreter, so python -m pip installs into .venv. The list command shows what is installed there. You will see requests plus the packages it depends on, such as urllib3 and certifi. pip installs those dependencies automatically.
Use the installed package
Save this as fetch.py and run it with python fetch.py while the environment is active.
import requests
response = requests.get("https://www.python.org")
print(response.status_code)A status code of 200 means the request succeeded. This needs an internet connection.
How pip finds and places a package
The packages end up in a site-packages folder inside .venv. Because they are inside the project's environment, other projects and the system Python are unaffected.
Common pip commands
| Task | Command |
|---|---|
| Install a package | `python -m pip install requests` |
| Install a specific version | `python -m pip install "requests==2.31.0"` |
| Upgrade a package | `python -m pip install --upgrade requests` |
| Remove a package | `python -m pip uninstall requests` |
| Show package details | `python -m pip show requests` |
| Save installed packages to a file | `python -m pip freeze > requirements.txt` |
| Install from a saved file | `python -m pip install -r requirements.txt` |
The requirements.txt file lets someone else recreate your environment: they create a fresh virtual environment and run python -m pip install -r requirements.txt.
Common errors
ModuleNotFoundError: No module named 'requests': The package is not installed in the interpreter running your script. Check that the environment is activated, then runpython -m pip list.No module named pip: Some Linux systems ship Python without pip. Install your distribution's pip package, or use a virtual environment, which includes pip.externally-managed-environment: Some systems block installing into the system Python. Create a virtual environment and install there instead of forcing the install.pip: command not found: Usepython -m pipinstead, or on Windowspy -m pip.
Check which Python pip is using
If something seems off, ask pip where it lives:
python -m pip --versionThe output includes a path. In an activated environment, that path should contain .venv. If it does not, the environment is not active.
Exercise
You activated .venv and ran pip install requests, but your script still raises ModuleNotFoundError. Name one likely cause and the command you would use to avoid it.
Show answer
The bare pip command probably belongs to a different Python than the one running your script. Use python -m pip install requests so the package installs into the same interpreter that runs the script. You can confirm with python -m pip list.
Exercise
What two commands let a teammate recreate your project's packages in a new environment?
Show answer
On your machine, python -m pip freeze > requirements.txt saves the list. In the teammate's new activated environment, python -m pip install -r requirements.txt installs it.