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
.venvper 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
.venvand build it again. It takes a minute.
It's a small habit, and it saves hours of "it worked yesterday".