Skip to main content
Basics

How to create a virtual environment in Python

Quick answer

Run python -m venv .venv, then activate it. This isolates your project's packages from your system Python.

# python -m venv .venv
# source .venv/bin/activate     # macOS / Linux
# .venv\Scripts\activate        # Windows PowerShell

# Now every pip install lands in .venv/, not system Python.
# python -m pip install requests

# When done:
# deactivate

venv is stdlib since Python 3.3 β€” no install needed. `python -m venv .venv` creates a folder called .venv containing an isolated Python + a private site-packages directory.

Activating (source .venv/bin/activate) sets your shell's PATH so `python` and `pip` point INTO the venv. Every install afterwards goes to .venv/, not the system. deactivate reverses this in the same shell.

Add `.venv/` to .gitignore β€” it's disposable and machine-specific (each dev recreates their own from requirements.txt or pyproject.toml). Never commit it.

Variations

uv-managed venv (2026 default)

# uv init         # creates pyproject.toml + .venv/
# uv add requests # installs into the .venv/
# uv run python foo.py  # runs inside the venv without activate

uv handles everything β€” no manual activate step needed.

Use a different Python version

# python3.13 -m venv .venv

Whatever Python you launch venv with becomes the venv's Python.

Check you're in the venv

# which python              # macOS/Linux
# where python              # Windows
# should show .venv/bin/python

First troubleshooting step when 'pip install' seems to hit the wrong place.

Practise this in your browser

Every how-to has a live lesson. Free tier, no card.

Start free β†’

Unlock every lesson

Pro β€” $12/mo, unlimited AI, cancel any time.

See Pro plans β†’

Get one Python lesson + one career idea every Friday

No spam, no "buy our course now". Three bullets, every Friday. Unsubscribe with one click.

Related how-tos