How to create a virtual environment in Python
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:
# deactivatevenv 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 activateuv handles everything β no manual activate step needed.
Use a different Python version
# python3.13 -m venv .venvWhatever 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/pythonFirst troubleshooting step when 'pip install' seems to hit the wrong place.
Get one Python lesson + one career idea every Friday
No spam, no "buy our course now". Three bullets, every Friday. Unsubscribe with one click.