Python Virtual Environments
Table of Contents
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.
cd myprojectThen create a virtual environment.
python -m venv venvThis 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:
venv\Scripts\activateOn Mac or Linux:
source venv/bin/activateAfter 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.
pip install requestsThat installs requests inside the virtual environment, not globally.
We can install multiple packages.
pip install requests flask numpyAll of them go into the virtual environment.
🗂️ How to Deactivate a Virtual Environment in Python
When we are done, we deactivate it.
deactivateThe 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.
pip freeze > requirements.txtThat file contains all the packages and their versions. We can share it with others.
To install from that file:
pip install -r requirements.txtThat 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.
mkdir myprojectcd myprojectpython -m venv venvActivate it.
On Windows:
venv\Scripts\activateOn Mac or Linux:
source venv/bin/activateInstall packages.
pip install requestsNow create a Python file and use the package.
import requests
response = requests.get("https://api.github.com")print(response.status_code)Output
200When we are done, deactivate.
deactivateThat 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.
python3.10 -m venv venvThat 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.