DEV Community

Sameer Qaiser
Sameer Qaiser

Posted on

Python Virtual Environments: What They Are and Why You Need Them

πŸ“Œ Quick Info

  • Topic: Virtual environments in Python
  • Target Audience: Beginners who've installed packages with pip and hit problems
  • Goal: Understand why virtual environments exist and how to use one

1. Introduction

"For my first two weeks of Python, I installed every package globally. Then I had two projects that needed two different versions of the same library β€” and everything broke. That's when I learned about virtual environments."


2. The Problem (Installing Everything Globally)

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

Every package goes into your global Python installation.

Problem: Project A needs requests==2.28, but Project B needs requests==2.31. You can only have one version installed globally. One project breaks.


3. The Solution (A Virtual Environment Per Project)

cd my_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.


4. How It Works

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

  • Its own copy of Python
  • Its own pip
  • Its own list of installed packages

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

Nothing you install inside can affect other projects.


5. What You’ll See When It’s Active

After activating, your terminal prompt changes:

(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 the global python.


6. Real Examples (Two Projects, Two Versions)

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

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 conflict.


7. Saving Your Dependencies

When you finish a project, save what you installed:

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

Later, on a new machine, restore everything with

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

That’s how python projects share exact package versions.


8. Deactivating and Deleting

To deactivate:

deactivate
Enter fullscreen mode Exit fullscreen mode

To Delete: Just delete the venv/ folder. That's it. Nothing is left behind.


9. Common Mistakes Beginners Make

Mistake 1: Forgetting to activate

  • You install a package thinking you're in the environment, but you're not.
  • Check for (venv) in your prompt.

Mistake 2: Committing venv/ to Git

  • Add venv/ to your .gitignore.
  • It's huge and not meant to be committed.
  • Commit requirements.txt instead.

Mistake 3: Creating the environment in the wrong folder

  • Always cd into your project first.
  • Then run python -m venv venv.

10. What I learned

  • Virtual environments isolate packages per project
  • python -m venv venv creates one
  • Activate it before installing anything with pip
  • (venv) in your prompt means it's active
  • pip freeze > requirements.txt saves your dependencies
  • Always add venv/ to your .gitignore

11. 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)