Chapter DPython virtual environmentsPage 2 of 8

Python virtual environments

Set up interfaces and contracts

A virtual environment gives one project its own python executable and package directory, preventing unrelated projects from sharing accidental dependency state.

~13 minSetup

Before you start

Why this matters

Before running anything, predict one observable result from the case: a data project needs pandas while another project must remain untouched. Write the prediction beside the command or code line that should cause it. This makes the session an experiment rather than a transcription exercise.

1Learn the idea

Read

Setup the chapter step

The required setup is: a chosen Python 3 interpreter is available and the shell is in the project root. Confirm it before copying code. The contract separates input from output: the interpreter version and declared package requirements goes in, and an isolated interpreter that imports the requested package at a recorded version comes out. If either side is ambiguous, later debugging will chase the wrong layer.

The input contract is the interpreter version and declared package requirements. The visible result is an isolated interpreter that imports the requested package at a recorded version.

Read

Run the working example

python3 -m venv .venv
source .venv/bin/activate
python -m pip install pandas
python -m pip freeze > requirements.txt
python -c "import sys, pandas; print(sys.prefix); print(pandas.__version__)"

Expected evidence:

/project/.venv
2.x.x

The output may include version-specific details such as hashes, paths, fitted thresholds, or final decimal places. Compare the structural facts described here rather than copying placeholders. If the structure differs, stop and inspect the earliest unexpected line.

Read

Read it line by line

  1. python3 -m venv .venv asks that exact interpreter to create the environment.
  2. source changes shell variables so python resolves inside .venv on macOS and Linux.
  3. python -m pip ties pip to the currently selected Python and avoids a mismatched executable.
  4. freeze records resolved versions; the final command proves both interpreter location and import.

These lines form one chain: the interpreter version and declared package requirements becomes an isolated interpreter that imports the requested package at a recorded version. Change only one input first. When several values change together, you cannot tell which change caused the new behavior.

Read

Common errors and fixes

  • First failure: an unchanged sys.prefix after activation means the activation command or shell path is wrong. Re-run the smallest command that proves the repair.
  • Second failure: No module named pandas commonly means the editor or terminal is using a different interpreter. Preserve the failing input as a test when it represents a realistic mistake.
  • Misleading success: a failing build after copying requirements.txt can indicate the file pins packages unavailable for the new Python or operating system. A clean-looking final line cannot cancel contradictory intermediate evidence.

When debugging, copy the exact error text and inspect names, paths, shapes, types, and versions. Explain the cause in one sentence before changing code. That discipline prevents a guessed repair from creating a second defect.

Read

Evidence for this stage

Add .venv/ to .gitignore; environments contain machine-specific executables and consume unnecessary space. Review dependency updates and avoid installing untrusted packages with look-alike names.

For the current setup step, save the smallest useful evidence: the relevant command, its output, and the input that produced it. Do not use a screenshot as the only record when text can be copied and searched. Keep generated artifacts separate from source inputs so rerunning the example does not destroy the evidence it is meant to evaluate.

Read every command or statement before running it. The examples deliberately expose intermediate state so a surprising result has somewhere concrete to point.

Read

Reflect on the result

Return to your opening prediction. Mark it correct or rewrite it with the condition you missed. Then explain the difference between a successful execution and a trustworthy result for this specific example.

Checking tutor…

Continue learning · glossary & guides
  1. Which line or command establishes the current step's most important fact?
  2. What output would reveal that No module named pandas commonly means the editor or terminal is using a different interpreter?
  3. Can a new user reproduce a rebuildable .venv based on a committed requirements.txt, not a committed environment directory from the stated setup?