Virtual environments and packages
Overview
The standard library is big, but the real superpower of Python is PyPI, the Python Package Index: over half a million free packages — web frameworks, data tools, API clients, game engines. This lesson shows how to install them safely, using a virtual environment for each project.
Why virtual environments?
Imagine two projects: an old one that needs Django 4.2, and a new one that needs Django 5.2. If you install packages into your computer’s single Python, they fight: installing one version replaces the other. And installing lots of packages globally slowly turns your system Python into a mess no one can reproduce.
A virtual environment (“venv”) is a folder containing a private set of packages for one project:
Each project gets exactly the versions it needs, and deleting a project’s .venv folder removes them cleanly.
Rule of thumb: one virtual environment per project, always.
Creating and activating
In your project folder:
python -m venv .venvThis creates a .venv folder (the name is a convention) containing a copy of the Python launcher and an empty site-packages.
Activate it so that python and pip in this terminal use it:
# macOS / Linux
source .venv/bin/activate
# Windows (PowerShell)
.venv\Scripts\Activate.ps1
# Windows (cmd)
.venv\Scripts\activate.batYour prompt changes to show (.venv). Check with:
python -c "import sys; print(sys.prefix)" # points inside .venvTo leave, type deactivate. Activation only affects the current terminal window.
VS Code detects .venv automatically and asks whether to use it; choose it (or use Python: Select Interpreter), and the Run button and new terminals will use the environment.
Installing packages with pip
With the environment active:
python -m pip install requests # latest version
python -m pip install "django==5.2.*" # a specific version series
python -m pip install --upgrade requests # upgrade
python -m pip uninstall requests
python -m pip list # what's installedUsing python -m pip (rather than plain pip) guarantees you’re using the pip that belongs to the Python you’re running.
Then use the package like any module:
import requests
response = requests.get("https://api.github.com/repos/python/cpython")
print(response.json()["stargazers_count"])Packages install their own dependencies automatically — requests brings urllib3, idna and others.
“externally-managed-environment”
On recent Linux distributions and Homebrew Python, pip install outside a virtual environment fails with error: externally-managed-environment. That’s deliberate: it protects the system’s own Python. The fix isn’t to force it — it’s to use a virtual environment.
Recording dependencies
Anyone who wants to run your project needs the same packages. Record them:
requirements.txt — the classic way
python -m pip freeze > requirements.txt # save exact versions
python -m pip install -r requirements.txt # recreate them elsewherecertifi==2026.11.20
charset-normalizer==3.5.3
idna==3.21
requests==2.34.2
urllib3==2.8.0pyproject.toml — the modern standard
Modern projects describe themselves in a pyproject.toml file:
[project]
name = "weather-bot"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"requests>=2.34",
]python -m pip install -e . installs the project and its dependencies in “editable” mode. Tools like uv (below) manage this file for you.
Don’t commit .venv
Add .venv/ to your .gitignore. Commit requirements.txt or pyproject.toml instead — the environment can be rebuilt from them on any machine, while a .venv folder only works on the computer that made it.
uv: the fast modern tool
uv is a newer tool that replaces venv, pip and more in one very fast program. Many teams now use it by default:
uv init weather-bot # create a project with pyproject.toml
cd weather-bot
uv add requests # creates .venv, installs, updates pyproject.toml and uv.lock
uv run main.py # run inside the project's environment — no activation needed
uv python install 3.15 # it can even install Python versions
uvx ruff check . # run a tool without installing it permanentlyuv writes a lock file (uv.lock) recording the exact versions of everything, so every machine gets an identical environment. pip and venv are always available and worth understanding; uv is worth adopting once you’re comfortable.
Choosing packages
Before adding a dependency, check its PyPI page and repository: is it maintained, widely used, and documented? Every package you add is code you’re trusting. Copy package names carefully — attackers publish look-alike names (“typosquatting”) hoping for a typo.
The workflow: new project →
python -m venv .venv→ activate →python -m pip install …→ record dependencies → never commit.venv. Or simply:uv init,uv add,uv run.
Set up a project environment
In a new folder: create a virtual environment called .venv, activate it, and install the requests package. Then write check_env.py, which prints whether it is running inside a virtual environment (compare sys.prefix with sys.base_prefix), which Python is running (sys.executable), and the installed version of requests — or requests is not installed if importing it fails. Run it with the environment active, then deactivate and run it again with your system Python to see the difference. Finally, save your dependencies with python -m pip freeze > requirements.txt.
# In the terminal:
# python -m venv .venv
# source .venv/bin/activate (Windows: .venv\Scripts\activate)
# python -m pip install requests
# check_env.py
import sys# check_env.py
import sys
from importlib.metadata import PackageNotFoundError, version
in_venv = sys.prefix != sys.base_prefix
print("Inside a virtual environment:", in_venv)
print("Python:", sys.executable)
try:
print("requests", version("requests"))
except PackageNotFoundError:
print("requests is not installed")Summary
- A virtual environment is a private folder of packages for one project:
python -m venv .venv. - Activate it (
source .venv/bin/activate, or.venv\Scripts\activateon Windows) andpythonandpipuse it. - Install packages from PyPI with
python -m pip install name; record them in requirements.txt or pyproject.toml. - Never commit
.venvto Git — commit the dependency list and recreate the environment anywhere. - uv (
uv init,uv add,uv run) does all of this much faster and also installs Python versions.