5.3. Virtual Environments
Table of Contents
Why Virtual Environments Matter
When you build Python backend applications, you almost never work with "plain Python" only. You install many external packages: FastAPI, SQLAlchemy, pytest, etc.
If you install everything globally on your system, you quickly get into trouble:
- Different projects need different versions of the same package.
- Updating a package for one project can break another.
- Your system Python becomes messy and hard to debug.
- Reproducing your setup on another machine is difficult.
A virtual environment is an isolated Python environment for one project.
Each project gets its own Python, its own pip, and its own installed packages.
A virtual environment is an isolated directory that contains:
- Its own Python interpreter
- Its own
pipcommand - Its own copy of installed packages
It does not change, replace, or "break" your system Python.
Using virtual environments is considered a must for modern Python development, especially for backend work.
Key Concepts
Global Python vs Virtual Environment
You usually have one or more global Python installations:
- On Linux or macOS: often preinstalled.
- On Windows: installed from python.org or Microsoft Store.
Global Python lives in system locations, for example:
- Linux:
/usr/bin/python3 - macOS:
/usr/local/bin/python3or viapyenv,brew, etc. - Windows:
C:\Python311\python.exeor similar
A virtual environment is simply a directory inside your project, for example:
myproject/venv/myproject/.venv/
This directory contains:
- A copy or link to Python.
- A
bin/orScripts/folder withpythonandpip. - A
site-packages/folder where all project dependencies are installed.
Why Isolation Is Important
Isolation solves several common problems:
| Problem | Without venv | With venv |
|---|---|---|
| Different project dependencies | Conflicting versions | Each project has its own versions |
| Upgrading a package | Might break other projects | Affects only that project |
| Reproducing environment elsewhere | Hard to remember everything | Use requirements.txt with that venv |
| Cleaning up unused packages | Risky to remove globally | Just remove the project or its venv |
Creating Virtual Environments
Python includes a built-in module for this: venv.
There are other tools, but venv is standard and enough for now.
Basic Creation Command
From your terminal or command prompt, go to your project directory:
cd /path/to/your/projectThen run:
python -m venv venvThis:
- Uses your current
pythoninterpreter. - Creates a folder called
venvinside your project.
You can use another name instead of venv, for example .venv:
python -m venv .venv
Both are common. .venv is popular because the leading dot often hides it from file explorers, and many tools detect it automatically.
What Gets Created
On Linux / macOS (with python -m venv venv):
myproject/
venv/
bin/
python
pip
activate
lib/
python3.x/
site-packages/
pyvenv.cfg
your_code.pyOn Windows:
myproject/
venv/
Scripts/
python.exe
pip.exe
activate.bat
Lib/
site-packages/
pyvenv.cfg
your_code.py
You never need to touch most of these files manually.
You mostly care about:
venv/bin/pythonorvenv\Scripts\python.exevenv/bin/piporvenv\Scripts\pip.exe- The activation script (
activate,activate.bat, etc.)
Activating and Deactivating
You can work with a virtual environment with or without activation, but activation makes things much more convenient.
Activating on Linux / macOS
From your project directory:
source venv/bin/activate
or if you used .venv:
source .venv/bin/activateAfter activation, your prompt usually changes, for example:
(venv) user@machine:~/myproject$Now when you type:
pythonor:
pipyou are using the versions inside the virtual environment, not the global ones.
Activating on Windows (Command Prompt)
From your project directory:
venv\Scripts\activate
or if you used .venv:
.venv\Scripts\activateYou should see:
(venv) C:\path\to\myproject>
Now python and pip will point to the virtual environment.
Activating on Windows (PowerShell)
From your project directory:
.\venv\Scripts\Activate.ps1or:
.\.venv\Scripts\Activate.ps1If you get an execution policy error, you may need to allow running local scripts, for example:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSignedThen try activating again.
Deactivating
To leave the virtual environment on any platform:
deactivate
Your prompt will return to normal, and python / pip will again be the global ones.
Rule
Always check your prompt for (venv) or (.venv) before:
- Installing packages with
pip install - Running your Python application
- Running tests
If you forget to activate, you may install packages globally by mistake.
Using `pip` Inside a Virtual Environment
Once the virtual environment is activated, the pip command uses the venv by default.
Example on any platform:
(venv) pip install fastapi uvicorn
This will install fastapi and uvicorn only inside venv.
You can check where pip is installing packages:
(venv) which pip # Linux / macOS
(venv) where pip # WindowsYou can also check installed packages:
(venv) pip listInstalling Specific Versions
To control versions clearly:
(venv) pip install fastapi==0.111.0
(venv) pip install "uvicorn[standard]==0.27.0.post1"This version pinning is critical when building backend APIs that must behave the same in development and production.
Checking Which Python You Are Using
It is useful to verify that you are using the venv Python:
On Linux / macOS:
(venv) which pythonOutput will be something like:
/home/user/myproject/venv/bin/pythonOn Windows:
(venv) where pythonOutput will be something like:
C:\path\to\myproject\venv\Scripts\python.exe
If it points to a system path like /usr/bin/python3 or C:\Python311\python.exe while you expect the venv, you are either not activated or something is misconfigured.
Virtual Environments and Project Structure
A common and practical structure for backend projects is:
myproject/
.venv/ # or venv/
app/
__init__.py
main.py
...
tests/
requirements.txt
README.mdWhy keep `venv` inside the project?
Pros:
- Easy to see the environment tied to the project.
- Simple to delete: remove the folder to start fresh.
- Many editors automatically detect
.venvinside the project.
Cons:
- The folder can be large, so you must ignore it in Git.
Ignore Virtual Environment in Git
Create a .gitignore file in your project and add:
venv/
.venv/
So Git does not track the virtual environment.
You never commit the venv to your repository.
Recreating Environments with `requirements.txt`
To share dependencies between machines, you do not share the venv itself.
You share a list of packages instead.
Common pattern:
- Install packages inside your venv:
(venv) pip install fastapi uvicorn
(venv) pip install sqlalchemy psycopg2-binary- Freeze current dependencies to a file:
(venv) pip freeze > requirements.txt
Now requirements.txt might contain:
fastapi==0.111.0
uvicorn==0.27.0.post1
SQLAlchemy==2.0.30
psycopg2-binary==2.9.9- On another machine (or after deleting the venv), you can recreate it:
python -m venv venv
source venv/bin/activate # or venv\Scripts\activate on Windows
pip install -r requirements.txt pip install -r requirements.txt
Rule
To reproduce a Python environment:
- Create a new virtual environment.
- Activate it.
- Install packages using:
Common Mistakes and How to Avoid Them
1. Installing Packages Globally by Accident
Symptom:
pip install fastapiseems to work.- Inside your code,
import fastapifails or works differently. pip listinside venv does not show the package.
Cause: You installed the package without activating the venv.
How to fix:
- Activate the virtual environment.
- Run
pip installagain.
2. Running the Wrong Python
Symptom:
python app/main.pyuses different packages than expected.which pythonorwhere pythonpoints to system Python.
Cause: Virtual environment not activated, or you are in another shell.
Fix:
- Activate the virtual environment in that terminal.
- Always run
pythonfrom an activated venv, or call it explicitly:
venv/bin/python app/main.py # Linux / macOS
venv\Scripts\python.exe app\main.py # Windows3. Committing the venv Folder to Git
Symptom:
- Your repository is huge.
- Many platform-specific files are in Git.
Cause: venv/ or .venv/ not in .gitignore.
Fix:
- Add
venv/and.venv/to.gitignore. - Remove them from Git history if already committed.
Examples: Step by Step on Each Platform
Example on Linux / macOS
# 1. Create project folder
mkdir backend-example
cd backend-example
# 2. Create virtual environment
python -m venv .venv
# 3. Activate it
source .venv/bin/activate
# 4. Check Python and pip
which python
which pip
# 5. Install dependencies
pip install fastapi uvicorn
# 6. Save dependencies
pip freeze > requirements.txt
# 7. Run Python using this environment
python -c "import fastapi; print(fastapi.__version__)"
# 8. When done
deactivateExample on Windows (Command Prompt)
REM 1. Create project folder
mkdir backend-example
cd backend-example
REM 2. Create virtual environment
python -m venv venv
REM 3. Activate it
venv\Scripts\activate
REM 4. Check Python and pip
where python
where pip
REM 5. Install dependencies
pip install fastapi uvicorn
REM 6. Save dependencies
pip freeze > requirements.txt
REM 7. Run Python using this environment
python -c "import fastapi; print(fastapi.__version__)"
REM 8. When done
deactivateVirtual Environments in Editors and IDEs
Modern editors and IDEs can detect and use your virtual environment automatically.
VS Code
Typical steps:
- Create a venv, for example
.venv. - Open the project folder in VS Code.
- In the bottom status bar, click on the Python version.
- Choose the interpreter inside
.venv, for example: .venv/bin/pythonon Linux / macOS.venv\Scripts\python.exeon Windows
VS Code will then:
- Use the venv for running and debugging.
- Use venv packages for IntelliSense and autocompletion.
PyCharm
When creating or importing a project, PyCharm can:
- Detect an existing virtual environment.
- Or create a new one in
.venvor another folder.
You usually set this in "Project Interpreter" settings.
When to Create a New Virtual Environment
A good rule:
Create a new virtual environment for every new Python project.
You typically create a venv when:
- You start a new backend application or service.
- You clone someone else's project repository.
- You want to try a new library in isolation.
You usually do not create a new venv:
- For every small script that is part of the same project.
- For standard library experiments that do not use external packages.
Summary
In this chapter you learned:
- What a virtual environment is and why it is essential for backend work.
- How to create one using
python -m venv venvorpython -m venv .venv. - How to activate and deactivate it on Linux, macOS, and Windows.
- How to use
pipinside the virtual environment to install project-specific dependencies. - How to reproduce environments with
requirements.txt. - How to avoid common mistakes like installing packages globally by accident or committing
venv/to Git.
You will use virtual environments in almost every Python backend chapter that follows, including FastAPI, databases, testing, and deployment, so practice these steps until they feel natural.
Views: 9
KAHIBARO