Modules and Packages · Lesson 26 of 39
pip, Packages and Virtual Environments
Python's real superpower is its ecosystem: more than half a million free packages on PyPI, the Python Package Index. Want to call a web API, build a dashboard or train a model? Somebody has already written the hard part. This lesson shows how to install packages properly – inside a virtual environment, so your projects never break each other.
In this lesson
- Why every project needs its own virtual environment
- Creating and activating a
venv - Installing, listing and removing packages with pip
- Sharing dependencies with
requirements.txt
The problem virtual environments solve
Imagine Project A needs version 1 of a library and Project B needs version 2. If both use the one Python installed on your computer, upgrading for B breaks A. A virtual environment is a private, self-contained folder of packages for one project.
One Python, many isolated projects
Python installed on your computer
- weather-app/.venvrequests 2.x, rich
- ml-project/.venvnumpy, pandas, scikit-learn
- website/.venvdjango, pillow
Create and activate a venv
Inside your project folder, run:
python -m venv .venv
This creates a .venv folder with its own copy of Python and pip. Now activate it:
# Windows (PowerShell)
.venv\Scripts\Activate.ps1
# macOS / Linux
source .venv/bin/activate
Your prompt now starts with (.venv). While active, python and pip refer to the environment. Type deactivate to leave it. VS Code detects .venv automatically and offers to use it.
Common mistake: "running scripts is disabled" on Windows
If PowerShell refuses to run Activate.ps1, allow local scripts for your user once with Set-ExecutionPolicy -Scope CurrentUser RemoteSigned, or use Command Prompt and run .venv\Scripts\activate.bat.
Installing packages with pip
pip install requests # latest version
pip install "rich==13.9.4" # an exact version
pip install --upgrade requests # upgrade
pip list # what is installed
pip show requests # details about one package
pip uninstall rich # remove
Then use the package like any module:
import requests
response = requests.get("https://api.github.com/repos/python/cpython", timeout=10)
data = response.json()
print(response.status_code, data["full_name"])
print("Stars:", data["stargazers_count"] > 10_000)
200 python/cpython Stars: True
Tip: python -m pip
If you have several Pythons installed, python -m pip install … guarantees that the package goes to the Python you are actually running. It avoids the classic "I installed it but Python says ModuleNotFoundError" problem.
requirements.txt: share your setup
Record the exact versions your project uses so teammates (and servers) can recreate the same environment:
pip freeze > requirements.txt # save
pip install -r requirements.txt # restore on another machine
# requirements.txt
certifi==2025.8.3
charset-normalizer==3.4.3
idna==3.10
requests==2.32.5
urllib3==2.5.0
Commit requirements.txt to Git, but never commit the .venv folder – add it to .gitignore. It is large, machine-specific and easy to rebuild.
The project workflow
Every new project
- mkdir project && cd projecta folder per project
- python -m venv .venvcreate the environment
- activateprompt shows (.venv)
- pip install …only what this project needs
- pip freeze > requirements.txtrecord versions; commit it
Modern tools: uv and pyproject.toml
The venv + pip combination is built in and works everywhere, which is why we teach it first. Many teams now also use uv, a very fast tool that creates environments, installs packages and manages Python versions in one command (uv init, uv add requests, uv run app.py). Projects increasingly describe their dependencies in a pyproject.toml file instead of requirements.txt. Once you understand environments, switching tools is easy – the ideas are identical.
Packages worth knowing
| Need | Package |
|---|---|
| Call web APIs | requests, httpx |
| Data analysis | pandas, numpy, polars |
| Charts | matplotlib, plotly |
| Web apps and APIs | django, flask, fastapi |
| Testing | pytest |
| AI and LLM apps | scikit-learn, torch, the anthropic and openai SDKs |
| Pretty terminal output | rich |
Practice
Try it yourself
Create a folder
weather-app, make a venv, activate it, installrequestsand save arequirements.txt.Show solution
mkdir weather-app cd weather-app python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate python -m pip install requests pip freeze > requirements.txtYour script says ModuleNotFoundError: No module named 'requests' although you installed it. List two likely causes.
Show solution
(1) The virtual environment is not activated, or VS Code is using a different interpreter. (2) pip belongs to a different Python than the one running the script – install with
python -m pip install requests.
Quick check
What is PyPI?
The Python Package Index – the public repository that pip install downloads packages from.
Should you commit the .venv folder to Git?
No. Commit requirements.txt (or pyproject.toml) and rebuild the environment from it.
What does pip freeze output?
Every installed package with its exact version, in requirements format.
Summary
- Create one virtual environment per project:
python -m venv .venv, then activate it. - Install with
python -m pip install name; check withpip list. - Save versions with
pip freeze > requirements.txt; never commit.venv.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.