← All posts

Python virtual environments: the short version

27 January 2025 · 1 min read
#python

Installing Python packages globally works until two projects need different versions of the same library. Virtual environments fix that.

The whole thing in four commands:

python -m venv .venv
source .venv/bin/activate      # Windows: .venv\Scripts\activate
pip install -r requirements.txt
deactivate

Habits that keep it tidy:

  • One .venv per project, inside the project folder.
  • Add .venv/ to .gitignore. Commit the list of packages, not the packages.
  • Pin versions in requirements.txt (or use a lock file) so a new release doesn't break you overnight.
  • Recreate rather than repair. If an environment gets messy, delete .venv and build it again. It takes a minute.

It's a small habit, and it saves hours of "it worked yesterday".

T
Written by Teza Wynn Htut

Cloud and infrastructure engineer in Auckland. I write down the fixes and lessons from things I build, and make tutorial videos on YouTube.

Watch on YouTube