Sub topic 2
Creating a virtual environment with python -m venv
This chapter assumes you can run commands in a terminal, run a Python script and import a module, and that Python 3 is on your command line. It covers creating an environment. Activation, package installs and the other topics get their own chapters.
What venv does
A virtual environment is a directory that holds its own Python interpreter entry point and its own site-packages folder. Packages installed into it stay there. Two projects can then use different versions of the same library without conflict.
venv is a module in the standard library, so nothing needs installing. You run it with the interpreter you want the environment to be based on:
python3 -m venv .venvThe argument is the directory to create. .venv is a common name. It is hidden on Linux and macOS, and many editors and tools look for it by that name.
On Windows, the py launcher does the same job:
py -m venv .venvWhy `python -m venv`
The -m flag runs a module as a script using that specific interpreter. The environment is built from whichever Python ran the command. If python3 is 3.12, the environment is a 3.12 environment. To pick another version, call that interpreter explicitly, for example python3.11 -m venv .venv.
What gets created
After the command finishes, the directory contains these items on Linux and macOS:
| Path | Purpose |
|---|---|
| `.venv/pyvenv.cfg` | Settings file that records the base Python and the environment options |
| `.venv/bin/` | The `python` and `pip` commands, plus the activation scripts |
| `.venv/lib/pythonX.Y/site-packages/` | Where installed packages go |
On Windows, the commands live in Scripts instead of bin, and packages go in Lib\site-packages. A later chapter examines these files in detail.
Checking that it worked
You can use the new interpreter directly, with no activation. Run it by path and compare two values:
python3 -m venv .venv
.venv/bin/python -c "import sys; print(sys.prefix); print(sys.base_prefix)"Inside an environment, sys.prefix points to .venv, while sys.base_prefix points to the Python installation it was built from. If the two values are equal, you are not running inside an environment. This comparison is the standard way to detect one.
Creation flow
Common options
--without-pipskips installing pip. This is rarely what you want.--clearempties an existing target directory before creating the environment.--system-site-packageslets the environment see packages from the base installation. Leave it off unless you have a specific reason, because it weakens isolation.--upgrade-depsupgrades pip in the new environment to the latest version.
Common errors
No module named venvon Debian or Ubuntu. The distribution packages it separately. Install thepython3-venvpackage, then retry.- Creating the environment in the wrong place. The path is relative to your current directory. Check with
pwdbefore running the command. - Moving or renaming the directory. Scripts inside an environment record absolute paths. A moved environment breaks. Delete it and create a new one in the right place.
- Using the wrong base Python. Check
python3 --versionbefore you create the environment. The version is fixed at creation.
Exercise
You run .venv/bin/python -c "import sys; print(sys.prefix == sys.base_prefix)" and it prints False. What does this tell you, and did you need to activate the environment first?
Show answer
It prints False because sys.prefix points to the .venv directory and sys.base_prefix points to the base installation, so the interpreter is running inside a virtual environment. No activation was needed, because calling the environment's interpreter by its path is enough to use it.