Virtual Environments
Learn how to give each Python project its own private sandbox so package versions never clash.
Imagine you are a chef working in two different restaurants at the same time. One restaurant needs you to use salt from Brand A, and the other insists on Brand B. You cannot mix them up — each kitchen needs its own supplies.
Python projects work the same way. Project A might need version 1.0 of a library, while Project B needs version 3.0. If you install everything in one global pile, they will eventually conflict. Virtual environments give every project its own private kitchen — its own isolated set of packages.
See it in action
— step through the idea, then dive into the details below.Every Project Gets Its Own Sandbox
Two projects can need different versions of the same package — and global installs can only hold one. Virtual environments give each project its own private space so versions never clash.
#The Problem: Global Package Chaos
When you first install Python, it comes with one global location where packages are stored. Every time you run pip install something, it lands there.
This seems fine at first — until you have two projects that need different versions of the same package. You cannot have both version 1 and version 3 installed globally at the same time. One of your projects will always break.
Think of it like apps on your phone
Each app on your phone runs in its own sandbox. Instagram cannot accidentally break WhatsApp because they share a file. Virtual environments do the same thing for Python projects — each one gets its own space.
#Creating a Virtual Environment
Python ships with a built-in tool called venv (short for virtual environment). You run it once inside your project folder and it creates a hidden folder — usually called .venv — that will hold all the packages for that project only.
# Run this in your terminal, inside your project folder
python -m venv .venvWhy the dot in .venv?
The leading dot (.venv) is a naming convention that marks the folder as hidden on macOS and Linux. It keeps your project folder tidy. You could name it anything, but .venv is the most common convention and many tools recognize it automatically.
#Activating the Environment
Creating the environment is not enough — you also need to activate it. Activation tells your terminal: "from now on, use the Python and pip inside .venv, not the global ones."
The command is slightly different depending on your operating system.
# macOS or Linux — run this in your terminal
source .venv/bin/activate
# Windows (Command Prompt)
.venv\Scripts\activate.bat
# Windows (PowerShell)
.venv\Scripts\Activate.ps1How do I know it worked?
After activating, your terminal prompt changes — it will show (.venv) at the beginning of the line, like this:
`` (.venv) your-computer:myproject you$ ``
That little label is your confirmation. You are now inside the sandbox.
#Installing Packages Inside the Environment
Once activated, pip install works exactly as you are used to — but now packages land inside `.venv` instead of the global location. They are completely isolated to this project.
# With the environment activated, install a package normally
pip install requests
# Verify it installed
pip listYou can also save the list of your project's packages to a file so teammates (or your future self) can recreate the exact same environment:
`` pip freeze > requirements.txt ``
Then someone else can install everything in one command:
`` pip install -r requirements.txt ``
#Leaving the Environment
When you are done working on a project, you can step out of the environment with a single word. This returns your terminal to using the global Python.
# Exit the virtual environment
deactivate#Keep .venv Out of Git
The .venv folder can contain thousands of files and take up megabytes of space. You should never commit it to Git. Instead, commit your requirements.txt — that tiny file tells anyone who clones your project exactly what to install.
Add .venv to your .gitignore file:
# Contents of your .gitignore file
.venv/
__pycache__/
*.pycForgetting to activate before installing
A very common mistake: you create the environment, then run pip install without activating first. The package ends up in the global location, not inside .venv.
Always check for the (.venv) label in your terminal prompt before running pip install. If you do not see it, run the source .venv/bin/activate command first.
Do not move the .venv folder
The paths inside .venv are hardcoded to your project's location. If you rename or move your project folder, the environment will break. Just delete .venv and run python -m venv .venv again — it only takes a few seconds.
#The Full Workflow at a Glance
# 1. Create the environment (once per project)
python -m venv .venv
# 2. Activate it (every time you start working)
source .venv/bin/activate # macOS/Linux
# .venv\Scripts\activate.bat # Windows
# 3. Install what you need
pip install requests pandas
# 4. Save your dependencies
pip freeze > requirements.txt
# 5. When done for the day
deactivateYou just created a virtual environment with `python -m venv .venv`. You then run `pip install flask`. Where does Flask get installed?
Key takeaways
- Virtual environments isolate each project's packages so version conflicts never happen.
- Create one with `python -m venv .venv` — just once per project.
- Always activate (`source .venv/bin/activate`) before installing or running code.
- Use `pip freeze > requirements.txt` to share your dependencies without committing `.venv`.
- Add `.venv/` to `.gitignore` — never commit the environment folder itself.
A developer runs these terminal commands in order. What does the prompt look like AFTER the third command succeeds?
# Command 1
python -m venv .venv
# Command 2
source .venv/bin/activate
# Command 3
pip install requestsA developer wants to install Flask into their virtual environment, but it ends up in the global Python location instead. What went wrong?
# In the terminal:
python -m venv .venv
pip install flaskComplete the two terminal commands: first to save the current environment's packages to a file, then to install from that file in a fresh environment.
# Save dependencies pip > requirements.txt # Recreate the environment elsewhere pip install requirements.txt
Put these terminal commands in the correct order to set up and use a new virtual environment from scratch.
pip install pandas
pip freeze > requirements.txt
source .venv/bin/activate
python -m venv .venv
deactivate
Create a new folder called sandbox-project. Inside it, create a virtual environment, activate it, install the httpx package, then save your dependencies to requirements.txt. Finally, open requirements.txt and paste its contents as a comment in a Python file called notes.py.
Try it live — edit the code and hit Run to execute real Python: