Python Virtual Environments

In the last lesson, we learned about Python Packages. Now let us isolate project dependencies with virtual environments and prevent version conflicts.

🧩 What Is a Virtual Environment in Python?

A virtual environment is a folder that contains a copy of Python and a separate space for installing packages. When we activate it, our terminal uses that Python instead of the system Python.

Think of it like a separate room for each project. Each room has its own tools. What we do in one room does not affect the other rooms.

🎯 Why Do We Need Virtual Environments in Python?

Different projects need different versions of packages. If we install everything globally, we might get conflicts. One project might need requests version 2.0, and another might need version 3.0. We cannot have both globally.

Virtual environments solve this. Each project gets its own environment with its own packages. No conflicts.

📦 How to Create a Virtual Environment in Python

Python comes with a module called venv for creating virtual environments.

First, open a terminal and go to your project folder.

Terminal window
cd myproject

Then create a virtual environment.

Terminal window
python -m venv venv

This creates a folder called venv inside our project. That folder contains the virtual environment.

We can name it anything. But venv is a common convention.

🔧 How to Activate a Virtual Environment in Python

To use the virtual environment, we need to activate it.

On Windows:

Terminal window
venv\Scripts\activate

On Mac or Linux:

Terminal window
source venv/bin/activate

After activation, we see the environment name in our terminal prompt, like (venv).

Now any Python command we run uses the virtual environment.

🛠️ How to Install Packages in a Virtual Environment

While the virtual environment is active, we use pip to install packages.

Terminal window
pip install requests

That installs requests inside the virtual environment, not globally.

We can install multiple packages.

Terminal window
pip install requests flask numpy

All of them go into the virtual environment.

🗂️ How to Deactivate a Virtual Environment in Python

When we are done, we deactivate it.

Terminal window
deactivate

The prompt goes back to normal. And we are using the global Python again.

⚙️ How to Save and Share Dependencies in Python

We can save a list of installed packages to a file called requirements.txt.

Terminal window
pip freeze > requirements.txt

That file contains all the packages and their versions. We can share it with others.

To install from that file:

Terminal window
pip install -r requirements.txt

That installs all the packages listed in the file. This is very useful when sharing projects.

🚀 A Real-Life Example – Setting Up a Project

Let us create a new project with a virtual environment.

Terminal window
mkdir myproject
cd myproject
python -m venv venv

Activate it.

On Windows:

Terminal window
venv\Scripts\activate

On Mac or Linux:

Terminal window
source venv/bin/activate

Install packages.

Terminal window
pip install requests

Now create a Python file and use the package.

import requests
response = requests.get("https://api.github.com")
print(response.status_code)

Output

200

When we are done, deactivate.

Terminal window
deactivate

That is a clean project setup.

💡 Common Mistakes with Virtual Environments in Python

📋 Mistake 1: Forgetting to activate the environment

If we do not activate it, pip install installs packages globally.

Fix: Always activate the environment before installing packages.

🔍 Mistake 2: Committing the venv folder to Git

The venv folder is large and specific to our machine. We should not commit it.

Fix: Add venv/ to .gitignore.

✅ Mistake 3: Using the wrong Python version

If we have multiple Python versions, we need to specify which one to use.

Terminal window
python3.10 -m venv venv

That uses Python 3.10.

⚠️ Mistake 4: Forgetting to save requirements

If we do not save requirements, others cannot reproduce our environment.

Fix: Run pip freeze > requirements.txt regularly.


Next, we will learn about the Python pip Package Manager and install, update, and remove packages.