Skip to main content

Python Virtual Environments

Fundamentals Deep Dive

Python Virtual Environments

Protect your global Python install and manage dependencies per project using virtual environments.

Create

python -m venv .venv
  • Use .venv as the folder to keep tooling happy (VS Code auto-detects it).
  • Commit a .gitignore entry for .venv/.

Activate

# macOS/Linux
source .venv/bin/activate

# Windows PowerShell
.venv\Scripts\Activate.ps1

Your prompt should show (.venv) to confirm activation.

Install dependencies

python -m pip install --upgrade pip
pip install requests
pip freeze > requirements.txt

Use pip-tools, poetry, or uv for locking/modern workflows if desired.

Deactivate

deactivate

Switch between environments per project. Never install dependencies globally.

Tips

  • Use pip install -e . for editable installs when building packages.
  • Automate activation via shell hooks or direnv for faster switching.

Next up in your learning path

Frequently Asked Questions

Do I need virtual environments when using Docker?

Yes for local development. Inside containers you can skip them because each container is already isolated, but local tooling should still rely on venv to avoid conflicts.

What's the difference between venv and conda?

venv ships with Python and manages pure-Python dependencies. Conda is a separate ecosystem that manages Python versions and compiled packages. Pick the tool that fits your workflow.

How do I remove a venv?

Deactivate it, delete the `.venv/` directory, and recreate it with `python -m venv .venv`. Regenerate requirements afterwards.