Python virtual environments
Define the lab goal and success criteria
A virtual environment gives one project its own python executable and package directory, preventing unrelated projects from sharing accidental dependency state.
1Try it yourself
Playground
Env conflict
Two projects need different package versions. A global install can’t please both.
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.
2Learn the idea
Read
Explain the chapter step
Begin by writing the success condition in observable terms. For this case, success is not familiarity with the vocabulary; it is producing a rebuildable .venv based on a committed requirements.txt, not a committed environment directory. Record the starting state so you can distinguish an improvement from a result that was already present.
On this page, the practical job is to state a measurable outcome before changing anything. The running case is a data project needs pandas while another project must remain untouched.
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
python3 -m venv .venvasks that exact interpreter to create the environment.sourcechanges shell variables sopythonresolves inside.venvon macOS and Linux.python -m pipties pip to the currently selected Python and avoids a mismatched executable.freezerecords 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.prefixafter activation means the activation command or shell path is wrong. Re-run the smallest command that proves the repair. - Second failure:
No module named pandascommonly 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.txtcan 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
Run python -m pip check to detect incompatible installed requirements, then deactivate and reactivate. Rebuilding in a second temporary folder from python -m pip install -r requirements.txt is the strongest local reproducibility test.
For the current explain 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.
Commit the dependency declaration and setup commands. State the supported Python version, because identical package pins can still behave differently across interpreter versions.
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.
Continue learning · glossary & guides
- Which line or command establishes the current step's most important fact?
- What output would reveal that an unchanged
sys.prefixafter activation means the activation command or shell path is wrong? - Can a new user reproduce a rebuildable
.venvbased on a committedrequirements.txt, not a committed environment directory from the stated setup?