بازگشت به مهندسی مدرن و کار با هوش مصنوعی · فصل ۵ — Shell، Workspace، Lockfile و Build Closure · 25

Python environments with uv and a target-scoped wheelhouse

Verify Python project lock/sync state and a target-scoped local wheelhouse from a clean offline environment without overclaiming OS/interpreter closure. می‌تواند «Python Environments با uv و Wheelhouse» را در یک workspace محدود طوری پیکربندی کند که dependency/toolchain state قابل تکرار و قابل بررسی باشد.

Python Environments با uv و Wheelhouse — fa-IR

Lesson summary

Verify Python project lock/sync state and a target-scoped local wheelhouse from a clean offline environment without overclaiming OS/interpreter closure.

می‌تواند «Python Environments با uv و Wheelhouse» را در یک workspace محدود طوری پیکربندی کند که dependency/toolchain state قابل تکرار و قابل بررسی باشد.

This lesson produces

می‌تواند «Python Environments با uv و Wheelhouse» را در یک workspace محدود طوری پیکربندی کند که dependency/toolchain state قابل تکرار و قابل بررسی باشد.

Who it is for

Foundation

Explicit boundary map, worked baseline, evidence prompts, and a safe local fixture.

Repair

Diagnose one misconception, reduce the fixture, rerun the relevant probe, then restore the full case.

Experienced

Skip command walkthroughs; require artifact, mutation evidence, tradeoff judgment, and residual-risk statement.

Prerequisites

A working .venv can hide a broken reconstruction path

A project has pyproject.toml, uv.lock, and a virtual environment that imports its dependency successfully. Then the only local wheel required by the target is removed. The warm environment still imports the package, yet a clean offline reconstruction cannot reinstall it. The green import proves the warm state works; it does not prove the environment can be recreated.

Your deliverable is Python Environment Closure Record v1. It binds project intent, lock freshness, Python and uv identity, target identity, local artifact inventory, clean-environment state, offline result, failure layer, and the remaining system boundary.

Three routes, one evidence bar

Foundation learners map each boundary before running commands. Repair learners isolate one stale-lock, interpreter, or artifact mismatch. Experienced learners may skip walkthroughs, but must retain hashes, tool/target identity, clean reconstruction evidence, a mutation, and residual-risk judgment.

Mental model: project → lock → interpreter/target → artifacts → clean environment

Keep five layers separate. pyproject.toml states dependency intent. uv.lock records a resolved project state but is not an OS or interpreter image. The selected Python interpreter and target platform determine compatibility constraints. A local artifact set must actually contain distributions compatible with that bounded target. Finally, reconstruction evidence must come from a clean environment; a warm .venv can preserve packages whose source artifacts are gone.

Native libraries, compilers, drivers, and other system resources remain outside this lesson unless they are explicitly represented as Python artifacts.

Build Python Environment Closure Record v1

Record pyproject_hash, uv_lock_hash, lock_fresh, python_identity, uv_identity, target_os_arch_abi, artifact_inventory_hash, clean_environment, offline_result, failure_layer, and residual_system_boundary. Label every material conclusion as OBSERVED, INFERRED, or UNKNOWN.

A defensible record answers two different questions: “Is the lock current for this project metadata?” and “Can this target reconstruct the locked environment from the evidence actually available locally?”

Current uv semantics used by this lesson

Current uv documentation distinguishes locking from synchronization. uv sync updates the project environment and performs an exact sync by default. The project is re-locked before sync unless --locked or --frozen is supplied. --locked checks that the lockfile is current and fails rather than changing it; --frozen uses the lockfile without checking whether project metadata has become stale. --offline disables network access and restricts uv to locally cached data and locally available files.

Those controls solve different problems. A frozen sync can intentionally ignore changed project metadata, so it is not evidence that the lock is fresh. An offline sync can still succeed from a warm cache, so the lab isolates cache state when proving local artifact sufficiency.

A wheelhouse is also bounded by compatibility. Wheels containing compiled code are commonly specific to OS, architecture, interpreter, and ABI. This lesson therefore records the target next to the artifact inventory instead of treating one wheel directory as universal.

Worked example: stale lock and missing artifact

Baseline: the fixture depends on demo-dep==1.0.0, uv.lock points to the packaged wheelhouse, and a clean environment with an isolated uv cache completes uv sync --locked --offline.

Stale metadata mutation: change the project requirement to demo-dep==2.0.0 without regenerating the lock. uv sync --locked --offline must not silently rewrite the lock; the correct evidence is a non-zero stale-lock result. --frozen would answer a different question because it trusts the existing lock without freshness validation.

Warm-state counterexample: restore the baseline, sync once, remove the wheel, and import from the existing .venv. The import can still succeed. Delete .venv, use a new empty uv cache, and retry the locked offline sync. Failure now locates the missing local-artifact boundary.

Four shortcuts that create false confidence

“uv.lock pins Python and the OS.” Repair: record interpreter and target identity separately.

“The import works, so recreation is proven.” Repair: destroy .venv and use a clean cache boundary.

“--frozen proves the lock is current.” Repair: use the documented locked/check semantics when freshness is the question.

“Our wheelhouse works everywhere.” Repair: bind the artifact inventory to an explicit target matrix and reject incompatible or missing wheels.

For experienced engineers: make the environment claim reviewable

In CI or release engineering, store target identity and artifact inventory beside the lock hash. When Python version, OS/architecture, dependency metadata, or native-extension requirements change, treat the previous closure result as target-scoped evidence that may need regeneration. A clean offline probe is especially valuable before declaring an air-gapped or disconnected deployment path ready.

Do not promote a successful install into a claim about application correctness. Installation closure and runtime correctness are separate evidence layers.

Exact reconstruction versus artifact cost

Maintaining one artifact set per supported target costs storage and build effort, but makes the claim auditable. A single broad wheelhouse is simpler until compiled wheels or Python-version boundaries appear. Universal pure-Python wheels can reduce duplication, yet that does not make every dependency universal.

Similarly, using caches speeds work but weakens the evidence value of a reconstruction test unless cache identity is controlled. For closure verification, a deliberately empty cache is often more informative than the fastest possible sync.

Lab: reconstruct from a clean local boundary

Use TOL004-LAB1. python setup_fixture.py creates a working copy inside the lab directory. python inspect_context.py records Python, uv, platform, paths, and artifact hashes. python run_uv_probe.py baseline performs a clean locked offline sync using an isolated cache and explicitly binds uv to the invoking Python with --python; retain the selected .venv interpreter identity returned by the probe. Then run the stale-lock and missing-artifact mutations independently, resetting between probes.

The packaged fixture requires no outbound network. If uv is unavailable, record UV_UNAVAILABLE; do not invent sync success or failure.

Minimum defensible evidence

Keep the exact project and lock hashes, executable identities, target tuple, the canonical artifact inventory hash (SHA-256 over sorted name<TAB>sha256<LF> lines), clean-environment declaration, offline command/result, mutation result, and residual system boundary. Distinguish LOCK_FRESHNESS, INTERPRETER_TARGET, LOCAL_ARTIFACT_AVAILABILITY, CLEAN_ENVIRONMENT, SYSTEM_RESIDUAL, and UNKNOWN_OR_MULTIPLE failure layers.

The strongest green result in this lesson is bounded: the locked project was reconstructable for the recorded target from the recorded local evidence under the recorded clean boundary. It does not prove another target, native system closure, or application correctness.

Assessment: diagnosis, decision, application, transfer

AS1 — Diagnosis: a warm .venv imports successfully after the wheel is removed. Identify the smallest evidence boundary that is still unproven.

AS2 — Decision: a team provides uv.lock and a wheel directory but no Python/target identity or clean reconstruction record. Decide ACCEPT, REJECT, or DEFER and state residual risk.

AS3 — Application: execute the local fixture, complete the closure matrix, and run stale-lock and missing-artifact mutations independently.

AS4 — Transfer: the target changes to a different Python/platform tuple and the supplied wheel set contains one incompatible or missing artifact. Decide whether the previous closure still holds and what must be regenerated.

Source and freshness boundary

Tool-specific claims are bounded to official uv and pip documentation rechecked on 2026-09-08. Current uv documentation still distinguishes lock freshness from frozen use, exact project sync, and offline local/cache behavior. Current pip documentation still describes wheelhouse installation bundles and warns that compiled wheels are commonly OS/architecture-specific.

The Bible review-due date is 2026-10-17. If command semantics or compatibility rules change, refresh the volatile source layer without expanding the lesson into OS/container closure.

Assessment

There is no answer key in this lesson pack; answer it yourself and mark honestly. "Needs review" sends the lesson to your review queue.

AS1 diagnosis

lesson ✗AI ✗notes ✗

AS2 decision

lesson ✗AI ✗notes ✗

AS3 application

lesson ✗AI ✗notes ✗

AS4 transfer

lesson ✗AI ✗notes ✗
Assessment rubric
  • Boundary correctness (critical): identify the smallest lock/interpreter/artifact/clean-environment boundary without expanding the claim.
  • Evidence traceability (critical): every conclusion cites a file/hash/command/identity or remains UNKNOWN.
  • Mutation robustness (critical): stale-lock and missing-artifact mutations change the conclusion appropriately.
  • Transfer & residual risk: adapt to a changed target and state what remains unproven.

Machine checks validate structure; the final engineering decision remains human-reviewed.

References & freshness

· TOOL_VOLATILE
Current uv project locking/synchronization semantics, including locked/frozen behavior.
· TOOL_VOLATILE
Current uv project environment and lockfile layout/boundary.
· TOOL_VOLATILE
Current uv CLI offline behavior and relevant flags.
· TOOL_VOLATILE
Current pip wheel command documentation used only for the bounded local-wheelhouse preparation concept.

Case studies

Lab files

  1. python setup_fixture.py
  2. python inspect_context.py to record launcher/tool identity and the canonical artifact inventory hash.
  3. python run_uv_probe.py baseline; retain both requested_python_identity and selected_environment_python_identity. The probe binds uv with --python to the invoking Python.
  4. Reset. Run python mutate_fixture.py stale-lock, then python run_uv_probe.py stale-locked.
  5. Reset. Run baseline, python mutate_fixture.py remove-wheel, then python run_uv_probe.py warm-import; finally run python run_uv_probe.py clean-after-remove.

Use no network. UV_UNAVAILABLE is missing runtime evidence and exits non-zero; do not convert it to PASS. The artifact inventory hash is SHA-256 of sorted UTF-8 lines name<TAB>sha256<LF>. Do not treat warm import as reconstruction evidence.

Lesson media & slides

MAKNOW_TOL-004_DECISION_EVIDENCE_TABLE_v1.0.0.svg
DECISION EVIDENCE TABLE v1.0.0.svg
MAKNOW_TOL-004_FAILURE_REPAIR_v1.0.0.svg
FAILURE REPAIR v1.0.0.svg
MAKNOW_TOL-004_PRIMARY_MODEL_v1.0.0.svg
PRIMARY MODEL v1.0.0.svg
MAKNOW_TOL-004_WORKED_EXAMPLE_v1.0.0.svg
WORKED EXAMPLE v1.0.0.svg

Teaching files (download)

XLSXPython Environment Closure Record v1 v1.0.0.xlsxTeaching files (download)
DOCXPython Environment Closure Record v1 WORKSHEET v1.0.0.docxTeaching files (download)
PDFPython Environment Closure Record v1 WORKSHEET v1.0.0.pdfTeaching files (download)