Python Virtual Environments Explained (venv in 10 Minutes)

By Naresh · October 24, 2026 · Tutorials

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.

Advertisement

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.

Advertisement

Habits that matter

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.

N
Naresh

Software developer (B.Tech, CSE) writing practical guides on streaming, web dev and developer tools — based on things actually built. Questions? Get in touch.

Need something like this built? I take freelance projects in streaming, web apps and automation. Get in touch.

More in Tutorials