The Complete Guide to Python Virtual Environments
If you have just started with Python, you have probably hit package installation errors or version conflicts. The standard solution is a virtual environment.
What is a virtual environment?
A virtual environment is an isolated folder that keeps each project’s Python version and packages separate from the rest of the system. The result: project A can use Django 4 while project B uses Django 5 — with no conflict at all.
Creating one with venv
python -m venv .venv
Activate it on Windows:
.venv\Scripts\activate
And on Linux or macOS:
source .venv/bin/activate
Key tips
- Always add the
.venvfolder to.gitignore. - Save your package list with
pip freeze > requirements.txt. - Create a fresh environment per project; a shared one brings the old problem right back.
With these three commands, package conflicts are a thing of the past.