When working on multiple Python projects, you'll often find that different projects need different versions of the same package. Without isolation, installing a package for one project can silently break another. A virtual environment solves this by giving each project its own separate space for installed packages, independent of your system's global Python installation and independent of every other project on your machine.
In this tutorial, you'll create a virtual environment, activate it, install packages inside it, and save those dependencies to a file so the environment can be recreated on another machine.
Prerequisites
Python 3.3 or later installed on your system (virtual environments are built into Python from this version onward)
Access to a terminal or command prompt
Basic familiarity with running commands in the terminal
To check your Python version, run:
bash
python3 --version
Step 1 — Creating the Virtual Environment
Navigate to your project's directory:
bash
cd my_project
Create a virtual environment named env:
bash
python3 -m venv env
This command creates a new directory called env inside your project folder. This directory contains a private copy of the Python interpreter along with a dedicated space for packages, completely separate from your system's global Python installation.
Step 2 — Activating the Virtual Environment
Creating the environment doesn't put you inside it — you need to activate it first.
On macOS and Linux:
bash
source env/bin/activate
On Windows:
bash
env\Scripts\activate
Once activated, your terminal prompt will change to show the environment's name in parentheses, like this:
bash
(env) user@machine:~/my_project$
This prefix confirms you're now working inside the isolated environment. Any package you install from this point forward stays inside env and has no effect on your system's global Python setup.
Step 3 — Installing Packages Inside the Environment
With the environment active, install a package as you normally would:
bash
pip install requests
To confirm it installed inside the environment and not globally, run:
bash
pip list
You'll see requests and its dependencies listed — and only the packages you've explicitly installed in this environment, not your system's full global package list.
Step 4 — Saving Your Dependencies
Once your project has the packages it needs, save the exact list to a file so the environment can be recreated later, whether by you on another machine or by anyone else who works on the project:
bash
pip freeze > requirements.txt
This creates a requirements.txt file listing each installed package with its exact version number, for example:
requests==2.31.0
Step 5 — Recreating the Environment From requirements.txt
On a new machine, or after cloning the project from version control, recreate the exact same environment:
bash
python3 -m venv env
source env/bin/activate
pip install -r requirements.txt
This installs every package listed in requirements.txt at the exact version specified, giving you an identical environment without manually reinstalling each package one at a time.
Step 6 — Deactivating the Environment
When you're done working, leave the virtual environment with:
bash
deactivate
Your terminal prompt returns to normal, and you're back to using your system's global Python installation.
Conclusion
You've created an isolated Python environment, installed packages inside it without affecting your system, saved those dependencies to a file, and learned how to recreate that exact environment elsewhere. This workflow is standard practice on every real Python project, and using it from your very first project will save you from dependency conflicts that are otherwise difficult to diagnose later.

Top comments (0)