If you install every Python package globally, sooner or later two projects will need different versions of the same library and one of them will break. A virtual environment solves this: each project gets its own private copy of Python packages. It takes thirty seconds to set up and it is built in to Python.
Step 1: Create the environment
Go to your project folder and run:
# Linux / macOS
python3 -m venv .venv
# Windows
py -m venv .venv
This creates a folder named .venv holding a private Python and a private place for packages. The name is only a convention. .venv is popular because editors detect it automatically.
On Debian and Ubuntu, if you get an error mentioning ensurepip, install the missing piece first:
sudo apt install python3-venv
To pick a particular Python version, create the environment with that interpreter. On Windows use the launcher: py -3.13 -m venv .venv. On Linux or macOS call the versioned program, for example python3.12 -m venv .venv. The environment keeps that version for its whole life.
What is inside the folder?
On Windows you will see Include, Lib, Scripts and pyvenv.cfg. On Linux and macOS the Scripts folder is called bin. Recent versions of Python also write a small .gitignore in there so the folder is not committed by accident. pyvenv.cfg is a short text file that records which Python the environment was built from and its version.
Step 2: Activate it
# Linux / macOS
source .venv/bin/activate
# Windows PowerShell
.venv\Scripts\Activate.ps1
# Windows Command Prompt
.venv\Scripts\activate.bat
Your prompt now starts with (.venv). From now on python and pip refer to the environment, not the system.
If PowerShell refuses with a message about running scripts being disabled, allow signed or local scripts for your user once:
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
Check that you are really inside it
The (.venv) prompt is a good sign, but you can also ask Python directly:
python -c "import sys; print(sys.prefix != sys.base_prefix)"
It prints True inside an environment and False outside. Two more quick checks: python -m pip --version shows a path inside your .venv folder, and which python (Linux/macOS) or where python (Windows) lists the environment’s Python first.
Step 3: Install packages
pip install requests
pip list
The package lands inside .venv only. Other projects and the system Python are untouched.
Use the environment without activating it
You do not have to activate an environment to use it. Call its Python directly and everything lands in the right place. This is the safest form in scripts, scheduled tasks and services:
# Linux / macOS
.venv/bin/python -m pip install requests
.venv/bin/python my_script.py
# Windows
.venv\Scripts\python.exe -m pip install requests
.venv\Scripts\python.exe my_script.py
Using python -m pip instead of plain pip guarantees that the pip belonging to that Python is the one that runs. A cron job or a service should always use the full path to the environment’s Python, for example /home/me/project/.venv/bin/python /home/me/project/job.py, because it does not run your shell’s activation.
Pin versions and keep pip current
Ask for an exact version with ==:
pip install "requests==2.32.3"
Installing a package also installs what it depends on, so pip freeze lists more than you asked for. For the example above it printed this when we ran it:
certifi==2026.10.23
charset-normalizer==3.5.2
idna==3.20
requests==2.32.3
urllib3==2.8.0
The versions you see will differ. Keeping the full frozen list in requirements.txt rebuilds the same environment later. Many teams also keep a short hand-written file with only the packages they chose.
When pip prints a notice that a newer version of pip itself is available, upgrade it inside the environment:
python -m pip install --upgrade pip
Step 4: Save and restore the list of packages
pip freeze > requirements.txt
On another machine (or a fresh clone of the project), recreate everything with two commands:
python3 -m venv .venv
.venv/bin/pip install -r requirements.txt
Commit requirements.txt to Git but not the .venv folder. Add this line to your .gitignore:
.venv/
Step 5: Leave or delete the environment
deactivate
To start over, just delete the folder. There is nothing hidden elsewhere:
rm -rf .venv # Linux / macOS
rmdir /s /q .venv # Windows Command Prompt
The “externally-managed-environment” error
Newer Linux distributions refuse pip install outside an environment and print error: externally-managed-environment. This protects system tools that depend on the system Python. The fix is not to force it with --break-system-packages. Create a virtual environment as shown above and install there.
Common problems and fixes
- “ModuleNotFoundError” right after installing. You installed into a different Python than the one running your code. Run
python -m pip --versionand compare the path with the Python you run. Activate the environment, or call its Python directly. - The editor does not see your packages. Point it at the environment. In Visual Studio Code press
Ctrl+Shift+P, choose Python: Select Interpreter and pick the one inside.venv. - You moved or renamed the project folder and the environment broke. The scripts inside an environment contain absolute paths, so it cannot simply be moved. Delete
.venvand recreate it fromrequirements.txt; it takes a minute. - You forgot which packages the project needs. Run
pip freezeinside the environment and compare it withrequirements.txt.
Which tool for which job?
| Tool | Use it for |
|---|---|
venv |
The built-in standard choice for project environments, as in this guide. |
pipx |
Installing command-line applications written in Python, each in its own hidden environment, so they never clash with your projects. |
virtualenv |
A third-party tool that does the same job as venv, with extra features and speed. |
| conda | Environments that can also hold non-Python software, common in data science. |
For most projects the built-in venv plus a requirements.txt file is all you need.
Quick reference
| I want to | Command |
|---|---|
| Create | python3 -m venv .venv |
| Activate (Linux/macOS) | source .venv/bin/activate |
| Install a package | pip install name |
| Upgrade a package | pip install --upgrade name |
| Save packages | pip freeze > requirements.txt |
| Install from the file | pip install -r requirements.txt |
| Leave | deactivate |
More tutorials like this
Python Tutorial for Beginners: A Complete Guide from Zero to Real Programs
A complete Python tutorial for beginners: variables, strings, lists and dictionaries, functions, files, classes, the standard library, testing and two small projects, with every…
Algorithms Tutorial: Big O, Sorting, Graphs and Dynamic Programming in Python
A full algorithms tutorial for beginners: Big O, searching, sorting, recursion, hashing patterns, trees, graphs (BFS, DFS, Dijkstra), greedy methods and dynamic programming, with…


