π How Python venv Works
venv (short for virtual environment) is Pythonβs built-in tool for creating isolated environments.
It lets you install dependencies on a per-project basis without affecting your system Python or other projects.
βοΈ What It Actually Isβ
A virtual environment is just a directory that contains:
.venv/
βββ bin/ # Executables (Python, pip, etc.)
β βββ activate # Shell script that sets environment vars
β βββ python # Copy/symlink of the Python interpreter
βββ lib/ # Site-packages live here
β βββ python3.x/
β βββ site-packages/
βββ pyvenv.cfg # Metadata (points to base Python install)
There is no background process or runtime service.
Itβs purely a self-contained folder structure.
π§© Lifecycleβ
| Step | Command | Description |
|---|---|---|
| Create | python3 -m venv .venv | Creates a new environment folder with a copy of Python. |
| Activate | source .venv/bin/activate | Temporarily adds .venv/bin to your PATH and sets VIRTUAL_ENV. |
| Deactivate | deactivate | Restores your shell to normal. |
| Delete | rm -rf .venv | Completely removes the environment and its packages. |
Activation only modifies the current shell session.
The environment itself persists until you delete it.
π¦ Installing Packagesβ
Once activated, installing packages with pip goes into the local environment:
source .venv/bin/activate
pip install requests
The package will be installed in .venv/lib/python3.x/site-packages/,
and will not affect your system Python or global pip environment.
π Quick Sanity Checkβ
which python
# β /path/to/project/.venv/bin/python
python -m site
# Shows local site-packages path inside .venv
If both point inside .venv, youβre isolated correctly.
π‘ Pro Tipsβ
-
Add
.venv/to your.gitignoreβ itβs per-user, not per-repo. -
If you use
requirements.txt, recreate the venv anytime by running:python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt -
Each project can have its own interpreter and dependency versions.
β
Summary:
venv is not a process β itβs a directory-based isolation mechanism.
It persists until you delete it and only changes your shellβs environment temporarily when activated.