Python Virtual Environments
If you install Python packages globally, things break sooner or later. One project needs a newer version, another needs an older one, and you end up fighting pip instead of writing code. Virtual environments solve this by keeping dependencies local to each project.
This post shows how to set one up and use it in a way that won’t get in your way later.
Install Python and venv
Make sure Python, pip, and the venv module are installed.
Linux (Debian/Ubuntu)
sudo apt update
sudo apt install python3 python3-venv python3-pip -y
Most systems already have Python, but python3-venv is sometimes missing.
Windows
Download and install Python from https://www.python.org/downloads/
During installation:
- Check “Add Python to PATH”
- Leave the rest as defaults
Verify installation:
python --versionpip --version
Create a project directory
Keep each project in its own folder. Put the virtual environment inside it.
mkdir -p ~/my_python_project
cd ~/my_python_project
Windows equivalent
mkdir my_python_project
cd my_python_project
The name doesn’t matter. The structure does.
Create the virtual environment
Create a virtual environment called venv:
python3 -m venv venv
Windows
python -m venv venv
This creates a venv/ directory with its own Python and pip. Anything installed here stays here.
Activate the environment
Linux / macOS
source venv/bin/activate
Windows (PowerShell)
venv\Scripts\Activate.ps1
Windows (Command Prompt)
venv\Scripts\activate.bat
You’ll see (venv) in your shell prompt. That’s how you know it’s active.
From this point on, pip install only affects this project.
Install packages
pip install --upgrade pip
pip install flask requests
Install whatever your project needs. It won’t touch system Python.
If you already have a requirements file:
pip install -r requirements.txt
Deactivate when you’re done
deactivate
That’s it. You’re back to the system environment.
Saving dependencies
When the project is in a good state, save the package list:
pip freeze > requirements.txt
To rebuild the environment later:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Windows rebuild
python -m venv venv
venv\Scripts\Activate.ps1
pip install -r requirements.txt
This works the same on another machine or server.
Important note (Windows only)
If PowerShell blocks activation scripts, run once as admin:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser