Public course previewThis is not your assigned passport. No identity, answers, or completion progress are stored here.How to start your passport
Handbook / labs

labs

Lab: Reproducible Python Environment

A Python environment keeps one project's interpreter and packages separate. Conda creates that environment; a small versioned definition file records how another person can recreate it.

Goal: create one isolated Conda environment, prove which interpreter is active, and make a test-first correction in the fictional Python practice project.

Prerequisites: your local Passport is open, the practice folder is ready, and the current Python mission names the project folder.

New to text commands? Read how to open a terminal and run one command before using a command block. It explains the labels used below and what to copy.

Execution context: use Windows PowerShell, macOS zsh, or Linux Bash on your local computer. Do not install Conda in Euler project or work storage.

The two Python missions own the exercise: environment setup, then test-first correction. This page is a standalone text companion, not another required checklist. Use its named section for explanation or recovery, then return to the mission.

1. Keep A Working Conda Installation#

Run:

conda --version

If it prints a version, keep that installation. If Conda was installed while this command terminal was open, including through Self Service, wait for the installer to finish. Keep the terminal running the Passport open. Close only the command terminal, open a new standalone PowerShell or Terminal window, and repeat the check before trying recovery or installation.

If Conda is still missing, follow the existing-installation checks. Install only after those checks find no existing installation and no managed installation is known. A managed installation outside the common folders needs IT setup help, not another download.

Do not install a second Conda distribution over an existing one and do not add Miniforge globally to Windows PATH when its installer warns against it.

2. Enter The Passport Practice Folder#

In the Create A Reproducible Python Environment mission, press Prepare practice folder and run the displayed enter-folder command in the new terminal. After a page reload, the same button reuses the folder and shows its path again. Follow the mission's Return to the practice folder and read the definition checks: the current folder and Git root must match the Passport path and the practice branch must be active. The return-to-folder guide explains recovery when a new terminal starts elsewhere.

git status --short --branch

Expected result: the Passport practice branch is active with no unexpected change.

3. Inspect Before Creating#

Use the platform-specific path check shown by the mission. It prints either ready-to-create or existing-conda-environment.

If .venv exists, do not replace it. The mission keeps it only when it is a real directory, Conda can read it, and its interpreter is Python 3.11. A file, symbolic link, Windows junction, incomplete environment, or different Python version stops the procedure without deleting anything.

The following read-only command is useful after the path checks pass:

conda list --prefix ./.venv

A valid existing environment can be reused. An unrecognized or incomplete path must be diagnosed before anyone decides whether it may be removed.

4. Create And Verify The Conda Environment#

First read the committed setup contract:

cat environment.yml

On Windows PowerShell, use Get-Content -LiteralPath environment.yml instead. The training file must name conda-forge, nodefaults, and Python 3.11. nodefaults excludes configured default channels when Conda resolves this file. If an earlier plugin check reports CondaToSNonInteractiveError, follow the channel-terms recovery instead of accepting terms or changing global channels.

Before creating anything, prove that Git ignores the target. --no-index checks the rule even while .venv is absent:

git check-ignore -v --no-index .venv/conda-meta/history

Expected result: Git prints the .gitignore rule for .venv/. Stop before creation if it prints nothing.

If the path inspection printed ready-to-create, create the environment once:

conda env create --prefix ./.venv --file environment.yml -y

If it printed existing-conda-environment, skip creation. After either safe path, activate and verify:

conda activate ./.venv
python -c "import sys; print(sys.executable); print(sys.version)"
python -m pip --version

The complete expected result is:

  • environment.yml names conda-forge, nodefaults, and Python 3.11;
  • Conda creates or reuses the environment inside the practice repository;
  • Git prints the .venv/ ignore rule;
  • the interpreter and pip paths contain the practice folder and .venv;
  • Python reports version 3.11.

The path is the proof. A shell prompt prefix alone is not sufficient.

If activation is unavailable, initialize the current shell once, close it, reopen it, return to the practice root, and activate again:

Windows PowerShell: conda init powershell
macOS zsh:          conda init zsh
Linux Bash:         conda init bash

5. Run The Unchanged Baseline#

cd workspace/python_project
python -m unittest discover -s tests -v

Expected result: the existing one-CPU test passes. If it does not, stop and repair the environment or baseline before editing source code.

6. Add A Failing Regression Test#

Read passport_example.py and tests/test_passport_example.py. Add one test with more than one CPU and independently calculate the expected total. Follow your Passport's versioned step: 3.0.0 accepts your chosen small integer example; 2.1.2 keeps its required total_memory_gib(4, 3) result of 12. Then rerun:

python -m unittest discover -s tests -v

Expected result: the new test fails because the current function returns the memory-per-CPU value instead of your calculated total. A failure for another reason means the test must be corrected first.

7. Make The Smallest Fix#

Change only the calculation so total memory equals CPU count multiplied by memory per CPU. Keep the existing validation for non-positive inputs, then run:

python -m unittest discover -s tests -v
cd ../..
git rev-parse --show-toplevel
git status --short
git diff --check
git diff -- workspace/python_project

The cd ../.. command returns from workspace/python_project to the practice root. Check that the printed Git root matches the path shown by Prepare practice folder. Stop if it differs; return using the Passport's enter-folder command before reviewing the diff.

Expected result: every test passes and the diff contains only the intended source and test change. .venv, caches, and generated files must not appear.

8. Run The Passport Checks#

Return to each Python mission and press Check my work. The environment check confirms the ignored Conda folder and interpreter. The code check runs the declared test suite using that interpreter.

Common Failures And Safe Recovery#

  • conda not found: open Miniforge Prompt or initialize the current shell, then restart the terminal.
  • Wrong interpreter: return to the practice root, activate ./.venv, and print sys.executable again.
  • Environment appears in Git: stop before staging and correct .gitignore.
  • New test passes before the fix: check that it uses more than one CPU and asserts the correct total.
  • Agent proposes dependencies: reject them; this practice project uses only the Python standard library.

Understand Before Accepting AI Output#

No agent is required for this lab. If one proposes a fix, first confirm the active interpreter, reproduce the failing test yourself, inspect the complete diff, and rerun the declared tests. Do not accept a dependency or environment change merely because it makes one command pass.

Completion Gate#

Continue only when both Passport checks pass, all tests pass in ./.venv, and you can explain the failing test and the smallest implementation change.

Ask For Help When#

An existing .venv cannot be identified, dependency declarations conflict, activation selects another Python, or recovery might delete unrelated work.