KKAIROS
Practical guideIllustrative3 min read

python -m venv: a separate package folder for one project

A venv gives a project its own prefix and site-packages while reusing the base interpreter. TE-22-A shows the prefix and base prefix only.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-22-A: a venv reuses the base interpreter but has its own prefix and site-packages

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

python3 -m venv v >/dev/null 2>&1; echo made=$?; v/bin/python -c "import sys; print('own_prefix', sys.prefix != sys.base_prefix); print('base', sys.base_prefix)"

Recorded output:

made=0
own_prefix True
base /usr

This run checks the exit status and records the output; it asserts no expected text.

TE-22-B: a new venv has a python program and a pyvenv.cfg file

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

python3 -m venv v >/dev/null 2>&1; test -x v/bin/python && echo has_python; test -f v/pyvenv.cfg && echo has_cfg

Recorded output:

has_python
has_cfg

Exit status: 0. This status and the output match what was written before the run.

TE-22-C: expected non-zero exit: importing a module that is not installed in the venv raises ModuleNotFoundError

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

python3 -m venv v >/dev/null 2>&1; v/bin/python -c "import nosuchmodule"

Recorded output: none, standard output was empty.

Standard error from the same run:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'nosuchmodule'

Exit status: 1. This status and the output match what was written before the run.

Reading the recorded output

The line made=0 is the exit status of python3 -m venv v, with its own output hidden. The next two lines were printed by v/bin/python, the interpreter inside the new folder. own_prefix True means sys.prefix and sys.base_prefix differ there. base /usr is the value of sys.base_prefix. The run does not print sys.prefix itself and does not list the site-packages folder.

TE-22-B: after python3 -m venv v, whose own output was discarded, v/bin/python is executable and v/pyvenv.cfg is a file, so has_python and has_cfg were printed. TE-22-C: by the setup, the venv has no module named nosuchmodule. The import stopped with exit status 1, and the traceback on standard error ends with ModuleNotFoundError: No module named 'nosuchmodule'.

Ad

Limits

TE-22-A shows the prefix and base prefix only. TE-22-B shows two files of a new venv and TE-22-C an import that fails. Installing packages and activating the environment are separate steps and were not tested here.

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-22-A | a venv reuses the base interpreter but has its own prefix and site-packages | 0 | PASS | dfe861ab00a4 | | TE-22-B | a new venv has a python program and a pyvenv.cfg file | 0 | PASS | 1cd79c266193 | | TE-22-C | expected non-zero exit: importing a module that is not installed in the venv raises ModuleNotFoundError | 1 | PASS | 5dd81f644f08 |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides