Python Virtual Environments Explained (venv in 10 Minutes)
You install a package with pip install requests and two weeks later another project fails because it needed a different version of the same library. That problem — and the "it works on my machine" family of bugs — is exactly what virtual environments solve. They take about ten minutes to understand and five seconds to use forever after.
The idea in one paragraph
A virtual environment is a private folder per project holding its own Python interpreter and packages. pip install inside the folder goes to the project; outside it, your system Python stays untouched. Two projects, two versions of the same library, zero conflict.
Step 1: create it
In your project folder:
python3 -m venv .venv
That creates a .venv directory (hidden, conventionally named). On Windows use py -m venv .venv.
Step 2: activate it
# macOS / Linux
source .venv/bin/activate
# Windows Command Prompt
.venv\Scripts\activate
Your prompt gains a (.venv) prefix — that is your signal that installs are now project-local.
Step 3: work inside it
pip install -r requirements.txt
python your_script.py
pip freeze > requirements.txt
pip freeze writes the exact versions you used into requirements.txt — the file teammates (and future you) need to reproduce the environment. Note the > in shell redirection; in a terminal you type a plain greater-than sign.
Step 4: leave it
deactivate
That is the whole lifecycle: create → activate → install → freeze → deactivate.
Habits that matter
- Add
.venvto.gitignore— environments are local and never belong in a repository - One environment per project, created from the project root, always
- Pick the interpreter in your editor: VS Code shows the notification automatically, or run Python: Select Interpreter and choose
.venv— otherwise you get confusing "package not found" errors - Recreate, don't repair: a corrupted environment is fixed by deleting
.venvand re-running steps 1-3
When to reach for more
venv is in the standard library and right for most work. Poetry or uv add dependency resolution and locking on top — worth it for libraries and teams, optional for scripts. And if you are automating this kind of setup, pair it with GitHub Actions so every push installs into a fresh environment.
Need something like this built? I take freelance projects in streaming, web apps and automation. Get in touch.