Why Python Virtual Environments are a Lifesaver (and How to Use Them)

Antima MishraAntima Mishra
4 min read

Story Pin image

Good morning! If you've spent any time working on more than one Python project, you've likely encountered this frustrating scenario: you install or update a package for Project A, and suddenly, Project B stops working. This problem, often called "dependency hell," happens because both projects are trying to use a shared, global collection of packages.

What if each of your projects could have its own private, isolated toolbox of packages, completely separate from all the others?

That's exactly what a virtual environment does. It's one of the most important best practices in the Python world, and in this guide, I'll show you how simple and powerful it is.

What Exactly Is a Virtual Environment?

Think of a virtual environment as a self-contained directory for your project that includes a specific version of Python and all the necessary packages your project needs to run.

It’s like giving each project its own clean, organized workspace. You can have one project that uses Django version 2.2 and another that uses Django 4.0 on the same computer, and they will never interfere with each other. This is essential for maintaining sanity and ensuring your projects are reproducible and easy to share.

Creating Your First Virtual Environment: A Step-by-Step Guide

Let's get hands-on. All you need is Python 3 installed. The module we'll use, venv, comes built-in.

Step 1: Create a Project Folder

First, create a new folder for your project and navigate into it.

Bash

$ mkdir my-python-project
$ cd my-python-project

Step 2: Create the Virtual Environment

Now, run the following command. This creates a new folder (we'll call it venv by convention) inside your project directory, which will contain all the magic.

Bash

# This tells Python to run the 'venv' module and create an environment named 'venv'
$ python3 -m venv venv

After running this, your folder will look like this:

my-python-project/
└── venv/

Step 3: Activate the Environment

This is the key step. Activating the environment tells your terminal to use the Python and pip versions inside the venv folder, not the global ones.

On macOS and Linux:

Bash

$ source venv/bin/activate

On Windows:

Bash

> venv\Scripts\activate

You'll know it worked because your terminal prompt will change to show the environment's name, like this:

Bash

(venv) $

Expert Tip: Any package you install now with pip will only be installed inside this (venv) environment. Your global Python installation will remain untouched.

Step 4: Install Packages

Let's install a package. The popular requests library is a great example.

Bash

(venv) $ pip install requests

It's now installed only in this environment.

Step 5: Deactivate the Environment

When you're finished working on your project, you can leave the environment by simply typing:

Bash

(venv) $ deactivate
$

Notice your prompt returns to normal. It's that easy to switch between projects!

The Professional Workflow: requirements.txt

Here's the final step that will make your projects professional and easy for others to use. You can create a list of all the packages your project needs.

While your environment is active, run this command:

Bash

(venv) $ pip freeze > requirements.txt

This creates a requirements.txt file that lists all packages and their exact versions. Now, when you share your project with someone else, they can recreate your exact environment with one simple command:

Bash

# This reads the file and installs all the listed packages
$ pip install -r requirements.txt

Conclusion: Make it a Habit

Virtual environments are a foundational concept in modern Python development. They keep your projects organized, prevent dependency conflicts, and make your work reproducible.

From now on, make it your #1 habit: every time you start a new Python project, start with a new virtual environment. It's a small step that will save you from countless headaches down the road.

What's a Python best practice that has saved you a lot of time? I'd love to hear about it in the comments!

0
Subscribe to my newsletter

Read articles from Antima Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Antima Mishra
Antima Mishra