DEV Community

Sameer Qaiser
Sameer Qaiser

Posted on

Python Virtual Environments: Why Every Project Needs One

📌 Quick Info

  • Topic: Virtual environments in Python
  • Target Audience: Beginners who've installed packages globally
  • Goal: Understand why isolation matters and how to set it up

1. Introduction

"For my first two weeks, I installed everything globally. Then two projects needed different versions of the same library — and both broke. That's when I learned about virtual environments."


2. The Problem (Global Installs)

pip install requests
pip install flask
pip install pandas
Enter fullscreen mode Exit fullscreen mode

Every package goes into your global Python installation.

What breaks:

  • Project A needs requests==2.28
  • Project B need requests==2.31
  • You can only have one globally installed at a time
  • One project breaks

This is called “dependency hell”. And it’s 100% avoidable.


3. The Solution (One Environment Per Project)

python -m venv venv

venv\Scripts\activate

source venv/bin/activate

pip install requests
Enter fullscreen mode Exit fullscreen mode

Now every project has its own isolated set of packages. No conflicts. Ever.


4. How It Works

A virtual environment is just a folder (venv/) that contains:

  • Its own copy of python
  • its own pip
  • its own installed packages

When you “activate” it, your terminal points to that folder instead of global Python.

Nothing inside can affect other projects.


5. How You Know It’s Active

Look at your terminal prompt:

(venv) C:\Users\You\my_project>
Enter fullscreen mode Exit fullscreen mode

That (venv) prefix means the environment is active. If you don’t see it, you’re using global Python


6. Real Example (Two Projects, Two Versions)

# Project A
cd project_a
python -m venv venv
source venv/bin/activate
pip install requests==2.28

# Project B
cd ../project_b
python -m venv venv
source venv/bin/activate
pip install requests==2.31
Enter fullscreen mode Exit fullscreen mode

Now both projects have the version they need. No conflicts.


7. Saving Your Dependencies

When you finish a project, save what you istalled:

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Later, on a new machine:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

That’s how Python projects share exact package versions.


8. Common Mistakes

Mistake Fix
Forgetting to activate Look for (venv) in the prompt
Committing venv/ to git Add venv/ to git.ignore
Creating it in the wrong folder cd into your project first
Deleting Python, not the venv Delete the venv/ folder only

9. What I learned

  • Virtual environments isolate packages per project.
  • python-m venv venv creates one
  • Activate it before installing anything
  • (venv) in your prompt means it’s active
  • Always add venv/ to git.ignore

10. Conclusion

"Virtual environments felt like extra steps for no reason — until the day two projects broke each other. Now I create one for every project, every time. It's five seconds of setup for hours of saved debugging.”

Top comments (0)