Python Virtual Environments
What you will learn
- The problem that virtual environments solve
- Creating one with
python -m venv - Activating and deactivating it
- Checking which environment is active
- What is inside the folder
- Good habits: one per project, not in git, easy to rebuild
1. The problem
Third-party packages (lesson 16.6) are installed into the Python that runs pip. If every project shares that one Python, trouble follows:
- Version conflicts. Project A needs
library 1.x, project B needslibrary 2.x. Only one version can be installed. - Clutter. On the computer these lessons were written on, the main Python lists over 200 installed packages after years of use. Nobody remembers which project needs which.
- Hard to share. You cannot tell a colleague which packages your project really needs.
- Risk. Upgrading something for one project can break another.
A virtual environment ("venv") solves this: a separate, private copy of the Python setup for one project, with its own site-packages folder (lesson 16.4). Packages installed inside it do not touch any other project or the system Python.
Quick check: what does a virtual environment isolate?
The installed third-party packages (and the pip that manages them) for one project, from every other project.
2. Creating an environment
The standard library includes the venv module. From the project folder:
python -m venv .venv
(On macOS and Linux you may need to type python3 instead of python.) This creates a folder named .venv (a common name; some people use venv or env). It takes a few seconds. Look inside it:
.venv/
├── pyvenv.cfg
├── Include/
├── Lib/ (site-packages is inside; "lib/" on Linux and macOS)
└── Scripts/ ("bin/" on Linux and macOS)
├── python.exe
├── pip.exe
├── activate
├── activate.bat
└── Activate.ps1
(This is the listing produced on Windows with Python 3.13; on Linux and macOS the folder is bin and the files differ.) pyvenv.cfg records which base Python the environment was created from:
home = C:\Users\ashiqur.rahman\AppData\Local\Programs\Python\Python313
include-system-site-packages = false
version = 3.13.7
include-system-site-packages = false means the environment does not see the system Python's installed packages: that is the isolation.
A brand-new environment contains almost nothing: pip list shows only pip itself.
> .venv\Scripts\python -m pip list
Package Version
------- -------
pip 25.2
3. Using it: two ways
a) Run its Python directly (simple and reliable)
Give the full path to the environment's own Python:
| System | Command |
|---|---|
| Windows | .venv\Scripts\python script.py |
| macOS / Linux | .venv/bin/python script.py |
Everything run this way uses the environment's packages. This is the least confusing way, and is how editors and scripts usually work. (The pathkori project's own notes use this form: .venv\Scripts\python manage.py test.)
b) Activate it
Activating changes your terminal session so that plain python and pip mean the environment's:
| Shell | Command |
|---|---|
| Windows, Command Prompt | .venv\Scripts\activate.bat |
| Windows, PowerShell | .venv\Scripts\Activate.ps1 |
| macOS / Linux (bash, zsh) | source .venv/bin/activate |
The prompt usually gains a prefix such as (.venv). Then:
(.venv) > python -m pip install tabulate
(.venv) > python script.py
(.venv) > deactivate
deactivate returns to the normal Python. On Windows PowerShell, you may need to allow scripts once (Set-ExecutionPolicy -Scope CurrentUser RemoteSigned); if that is a nuisance, use option (a). Activation affects only the terminal where you ran it, so a new terminal window starts without it.
Many editors (VS Code, PyCharm) can detect a .venv in the project folder and select it as the interpreter for you.
4. Which environment am I in?
Ask Python itself. This script, env_report.py, prints what it finds:
import sys
from pathlib import Path
in_venv = sys.prefix != sys.base_prefix
print("Python:", Path(sys.executable).name)
print("Virtual environment:", in_venv)
if in_venv:
print("Environment folder:", Path(sys.prefix).name)
try:
import tabulate
print("tabulate", tabulate.__version__)
except ModuleNotFoundError:
print("tabulate is not installed here")
Run with the main Python of this computer:
Python: python.exe
Virtual environment: False
tabulate is not installed here
Run with the environment's Python (.venv\Scripts\python env_report.py):
Python: python.exe
Virtual environment: True
Environment folder: .venv
tabulate is not installed here
After .venv\Scripts\python -m pip install tabulate, the same command prints:
Python: python.exe
Virtual environment: True
Environment folder: .venv
tabulate 0.10.0
- Inside a venv,
sys.prefix(the environment) differs fromsys.base_prefix(the Python it was made from). sys.executableis the full path of the running Python; print it in full when debugging.- The package version
0.10.0is what was current when this was written; yours may differ. - In a terminal,
where python(Windows) orwhich python(macOS and Linux) shows whichpythonis used.
5. Good habits
| Habit | Why |
|---|---|
| One environment per project | no conflicts between projects |
name it .venv in the project folder |
editors find it; easy to remember |
install with python -m pip |
guarantees the right Python (lesson 16.4) |
| never copy an environment folder to another computer | paths inside it are tied to its location; recreate it instead |
keep a requirements.txt (lesson 16.6) |
anyone can rebuild the same environment |
do not commit .venv to git |
it is large (about 13 MB in the check made for this lesson, with just pip and one small package), machine-specific, and can be rebuilt; add .venv/ to .gitignore |
| do not edit files inside it | they are managed by pip |
| delete and recreate when in doubt | it is safe: your code is not inside it |
Rebuilding is the whole point: delete the folder, run python -m venv .venv again, and install from requirements.txt. A broken environment costs a minute, not a day.
Other tools exist: virtualenv, pipenv, poetry, conda and uv all manage environments, some with extra features such as lock files or different Python versions. venv ships with Python, needs nothing installed, and is enough to learn the idea; the others work on the same principle.
Worked example: from nothing to a working project environment
You start a project report-tool. The commands (Windows form; for macOS and Linux replace .venv\Scripts\python with .venv/bin/python):
> mkdir report-tool
> cd report-tool
> python -m venv .venv
> .venv\Scripts\python -m pip install tabulate
> .venv\Scripts\python -m pip freeze > requirements.txt
> echo .venv/ > .gitignore
After the install, requirements.txt holds the single line (the version is the one installed in this check):
tabulate==0.10.0
A colleague (or you, on a new computer) starts from the code and rebuilds:
> python -m venv .venv
> .venv\Scripts\python -m pip install -r requirements.txt
> .venv\Scripts\python env_report.py
and gets the same report as above, ending with tabulate 0.10.0.
| Step | Purpose |
|---|---|
python -m venv .venv |
creates the private environment |
.venv\Scripts\python -m pip install ... |
installs into that environment only |
pip freeze > requirements.txt |
records exactly what is installed |
.gitignore entry |
keeps the heavy, rebuildable folder out of git |
rebuilding from requirements.txt |
the same setup anywhere |
The first four commands were run for this lesson in a temporary folder; the rebuild line is the standard command (pip install -r, lesson 16.6).
Common mistakes
- Installing packages with no environment active, into the main Python.
- Using the wrong
pip(a different Python from the one that runs your script). - Forgetting to activate in a new terminal.
- Committing
.venvto git. - Moving or copying an environment instead of recreating it.
- Mixing environments: the editor uses one, the terminal another.
- Naming your script
venv.py, which shadows the module (lesson 16.4).
Key takeaways
- A virtual environment is a private Python setup for one project, so packages and versions do not clash.
- Create it with
python -m venv .venv; run its Python directly (.venv\Scripts\python) or activate it and usedeactivateto leave. - Check with
sys.prefix != sys.base_prefixandsys.executable. - Install with
python -m pip, record withrequirements.txt, ignore.venvin git. - Never copy an environment: delete and rebuild it.
Practice
1. Which command creates a virtual environment named .venv?
Show answer
python -m venv .venv
2. How do you run script.py with the environment's Python without activating it (Windows)?
Show answer
.venv\Scripts\python script.py
3. How can a script tell that it is running inside a virtual environment?
Show answer
sys.prefix != sys.base_prefix.
4. Why should .venv not be committed to git?
Show answer
It is large, tied to one machine, and can be rebuilt from requirements.txt.
5. Your editor shows import errors for an installed package, but the terminal works. What might be wrong?
Show answer
The editor is using a different interpreter from the environment where the package is installed. Select the .venv interpreter in the editor.
6. What should you do if an environment seems broken?
Show answer
Delete the folder, create a new one, and reinstall from requirements.txt. Your own code is not inside the environment.