Python venv
last modified November 29, 2026 Python 3.15
In this article we show how to create and use Python virtual environments with
the venv module.
Applications depend on third-party packages, and different applications often
need different versions of the same package. A Python installation has only one
site-packages directory, so it can hold only one version of each
package. Installing everything there also mixes the packages of all our projects
and can break tools of the operating system that rely on the system Python.
A virtual environment is a directory that contains its own
site-packages directory and links to a Python interpreter. Packages
installed into it are visible only to programs that run in that environment.
Each project gets its own environment, and the base installation stays clean.
The venv module is part of the standard library. Editors such as
VS Code and PyCharm detect virtual environments and can create them as well.
Creating a virtual environment
A virtual environment is created with python -m venv followed by
the name of the directory. A common name is .venv; in this article
we use myenv to make the name visible.
$ python --version Python 3.15.2 $ python -m venv myenv $ ls -A myenv bin .gitignore include lib pyvenv.cfg
Directory layout
The bin directory contains the interpreter, pip, and
the activation scripts for several shells. On Windows the directory is called
Scripts.
The lib directory holds lib/python3.15/site-packages,
the place where packages of the environment are installed. The
include directory is for C headers of packages that need them. The
.gitignore file, added in Python 3.13, contains a single
*, so Git ignores the whole environment.
$ ls myenv/bin activate activate.csh activate.fish Activate.ps1 pip pip3 pip3.15 python python3 python3.15 $ ls myenv/lib/python3.15/site-packages pip pip-26.2.2.dist-info
The pyvenv.cfg file
The pyvenv.cfg file marks the directory as a virtual environment.
When Python starts, it looks for this file next to the executable or one
directory above it.
The home key points to the directory of the base interpreter,
which provides the standard library. The
include-system-site-packages key tells whether the packages of the
base installation are visible. The last keys record the Python version, the
real executable, and the command that created the environment.
$ cat myenv/pyvenv.cfg home = /home/user/.local/bin include-system-site-packages = false version = 3.15.2 executable = /home/user/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/bin/python3.15 command = /home/user/.local/bin/python -m venv /home/user/project/myenv
Activating an environment
Activation puts the bin directory of the environment at the start
of PATH, so python and pip refer to the
environment's programs. The activate script must be sourced, not
executed, because it changes the current shell. It also adds the name of the
environment to the prompt and sets the VIRTUAL_ENV variable.
On Windows the environment is activated with
myenv\Scripts\activate. The deactivate command,
defined by the activation script, restores the original PATH and
prompt.
$ source myenv/bin/activate (myenv) $ which python pip /home/user/project/myenv/bin/python /home/user/project/myenv/bin/pip (myenv) $ echo $VIRTUAL_ENV /home/user/project/myenv (myenv) $ deactivate $ which python /home/user/.local/bin/python
Detecting the environment
Inside Python, the sys.prefix variable points to the active
environment, while sys.base_prefix points to the base
installation. Outside a virtual environment, the two are equal.
import sys
print(f'executable: {sys.executable}')
print(f'prefix: {sys.prefix}')
print(f'base prefix: {sys.base_prefix}')
in_venv = sys.prefix != sys.base_prefix
print(f'in a virtual environment: {in_venv}')
The program prints the interpreter, both prefixes, and whether it runs inside a virtual environment.
in_venv = sys.prefix != sys.base_prefix
This comparison is the recommended way to detect a virtual environment. We run the program first with the environment active and then from a plain shell.
$ source myenv/bin/activate (myenv) $ python check_venv.py executable: /home/user/project/myenv/bin/python prefix: /home/user/project/myenv base prefix: /home/user/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu in a virtual environment: True (myenv) $ deactivate $ python check_venv.py executable: /home/user/.local/bin/python prefix: /home/user/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu base prefix: /home/user/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu in a virtual environment: False
Running without activation
Activation is only a convenience for interactive shells. Running the
interpreter of the environment by its path has the same effect, because Python
finds pyvenv.cfg next to the executable. This is the usual way to
run programs from cron jobs, systemd services, or scripts.
$ myenv/bin/python check_venv.py executable: /home/user/project/myenv/bin/python prefix: /home/user/project/myenv base prefix: /home/user/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu in a virtual environment: True
Installing packages
With the environment active, pip installs packages into its
site-packages directory. A new environment contains only
pip itself.
$ source myenv/bin/activate (myenv) $ pip list Package Version ------- ------- pip 26.2.2 (myenv) $ pip install humanize tabulate Collecting humanize Downloading humanize-4.17.0-py3-none-any.whl.metadata (8.2 kB) Collecting tabulate Downloading tabulate-0.10.0-py3-none-any.whl.metadata (40 kB) Downloading humanize-4.17.0-py3-none-any.whl (133 kB) Downloading tabulate-0.10.0-py3-none-any.whl (39 kB) Installing collected packages: tabulate, humanize Successfully installed humanize-4.17.0 tabulate-0.10.0 (myenv) $ pip list Package Version -------- ------- humanize 4.17.0 pip 26.2.2 tabulate 0.10.0
Environment isolation
The installed packages are available only to the environment's interpreter.
The following program uses the humanize package to format numbers
for people.
from datetime import timedelta import humanize print(humanize.intcomma(1234567)) print(humanize.naturalsize(3_500_000)) print(humanize.naturaldelta(timedelta(hours=26))) print(humanize.ordinal(23))
The program formats a large number, a file size, a duration, and an ordinal number.
import humanize
The import succeeds inside the environment. The base installation does not have the package, so the same program fails when we run it after deactivation.
$ source myenv/bin/activate
(myenv) $ python readable.py
1,234,567
3.5 MB
a day
23rd
(myenv) $ deactivate
$ python readable.py
Traceback (most recent call last):
File "/home/user/project/readable.py", line 3, in <module>
import humanize
ModuleNotFoundError: No module named 'humanize'
Requirements file
An environment is not meant to be copied or committed. Instead, we record the
installed packages and their versions in a requirements.txt file
and recreate the environment from it on another machine. The
pip freeze command prints the packages in the required format.
We then create a second environment, install the same packages with
pip install -r, and run the program with its interpreter.
$ source myenv/bin/activate (myenv) $ pip freeze > requirements.txt (myenv) $ deactivate $ cat requirements.txt humanize==4.17.0 tabulate==0.10.0 $ python -m venv env2 $ env2/bin/pip install -r requirements.txt Collecting humanize==4.17.0 (from -r requirements.txt (line 1)) Downloading humanize-4.17.0-py3-none-any.whl.metadata (8.2 kB) Collecting tabulate==0.10.0 (from -r requirements.txt (line 2)) Downloading tabulate-0.10.0-py3-none-any.whl.metadata (40 kB) Downloading humanize-4.17.0-py3-none-any.whl (133 kB) Downloading tabulate-0.10.0-py3-none-any.whl (39 kB) Installing collected packages: tabulate, humanize Successfully installed humanize-4.17.0 tabulate-0.10.0 $ env2/bin/python readable.py 1,234,567 3.5 MB a day 23rd
Custom prompt
By default, the activated prompt shows the name of the environment directory.
For an environment named .venv this is not very descriptive. The
--prompt option sets a different name; the special value
. uses the name of the current directory.
The prompt is stored in pyvenv.cfg. After activation it is also
available in the VIRTUAL_ENV_PROMPT variable.
$ python -m venv --prompt shop .venv $ grep prompt .venv/pyvenv.cfg prompt = 'shop' command = /home/user/.local/bin/python -m venv --prompt="shop" /home/user/project/.venv $ source .venv/bin/activate (shop) $ echo $VIRTUAL_ENV_PROMPT shop
System site packages
The --system-site-packages option gives the environment access
to the packages of the base installation. Packages installed into the
environment still go to its own directory and take precedence. This is useful
when the base installation provides large packages that are hard to build.
We print the last two entries of sys.path. The environment's
site-packages comes before the base installation's.
$ python -m venv --system-site-packages sysenv $ grep system sysenv/pyvenv.cfg include-system-site-packages = true command = /home/user/.local/bin/python -m venv --system-site-packages /home/user/project/sysenv $ sysenv/bin/python -c "import sys; print(*sys.path[-2:], sep='\n')" /home/user/project/sysenv/lib/python3.15/site-packages /home/user/.local/share/uv/python/cpython-3.15.2-linux-x86_64-gnu/lib/python3.15/site-packages
Creating environments in code
The venv module can also be used from Python. The
EnvBuilder class takes the same options as the command line and
creates the environment with its create method.
import subprocess
import venv
builder = venv.EnvBuilder(with_pip=True, symlinks=True, prompt='tools')
builder.create('toolenv')
python = 'toolenv/bin/python'
code = 'import sys; print(sys.prefix != sys.base_prefix)'
out = subprocess.run([python, '-c', code], capture_output=True, text=True)
print(f'in a virtual environment: {out.stdout.strip()}')
out = subprocess.run([python, '-m', 'pip', '--version'],
capture_output=True, text=True)
print(out.stdout.split(' from ')[0])
The program creates an environment called toolenv and checks it
by running its interpreter.
builder = venv.EnvBuilder(with_pip=True, symlinks=True, prompt='tools')
builder.create('toolenv')
The defaults of EnvBuilder differ from the command line. It does
not install pip unless with_pip=True, it copies the
interpreter instead of linking it unless symlinks=True, and it
writes no .gitignore file. A copied interpreter does not work with
every Python installation; with the uv-managed Python used here, the copy
cannot find the standard library and fails to start, so we request symlinks
as the command line does.
out = subprocess.run([python, '-c', code], capture_output=True, text=True)
We run the new interpreter as a child process. The current program keeps running in the base installation; creating an environment does not activate it.
$ python make_env.py in a virtual environment: True pip 26.2.2
Removing an environment
A virtual environment has no uninstaller and is not registered anywhere. To
remove it, we delete its directory. If a project moves to another directory,
we delete its environment and create a new one, because the scripts in
bin contain absolute paths, as the activation script shows.
$ grep "export VIRTUAL_ENV=" myenv/bin/activate
export VIRTUAL_ENV=/home/user/project/myenv
$ rm -rf myenv env2 .venv sysenv toolenv
$ ls
check_venv.py
make_env.py
readable.py
requirements.txt
Source
venv — Creation of virtual environments
Install packages in a virtual environment using pip and venv
In this article we have created Python virtual environments with
venv. We have activated them, installed packages, recorded them in a
requirements file, used the --prompt and
--system-site-packages options, and created an environment with
EnvBuilder. The uv tool offers a faster uv venv
command that creates compatible environments. Modules and packages are covered
in the module and
package articles; running Python in containers
is shown in Python Docker.
Author
List all Python tutorials.