Layer 8 Session 47 โ€” Gate Beyond the Fundamentals

Packaging & Virtual Environments

The final session. Every pip install across this curriculum quietly assumed a working Python setup. This session covers the practical piece every real project needs: isolating dependencies per-project.

global Python env/ pytest 8.3

An isolated, reproducible set of dependencies, per project.

Estimated time: 35โ€“40 minutes  ยท  Pre-quiz โ†’ Concept โ†’ Lab โ†’ Commit โ†’ Post-quiz

1. Learning Objective

By the end of this session you will be able to:

  • Explain what problem a virtual environment solves
  • Create and activate a virtual environment with venv
  • Install packages into an isolated environment and freeze them to a requirements.txt
  • Recreate an identical environment on a different machine from requirements.txt
  • Explain, at a high level, what pyproject.toml is for in a real distributable package

2. Pre-Coding Quiz

Answer these before reading the concept explanation or writing any code. It is fine to get these wrong โ€” that is the point. You need 4/5 to proceed to the lab.

Question 1 of 5

Two different projects on the same computer need different, conflicting versions of the requests library. What problem does this create if both use the same global Python installation?

A global Python installation has exactly ONE set of installed packages, shared by everything on that machine. Two projects needing different versions of the same library genuinely conflict โ€” this is exactly the problem virtual environments solve.
Question 2 of 5

What does a virtual environment (created with python -m venv env) actually provide?

A virtual environment is a self-contained directory holding its own Python interpreter (or a link to one) and its own site-packages โ€” installing something into it has zero effect on the global installation or any other project's environment.
Question 3 of 5

What does pip freeze > requirements.txt do, and why does the whole curriculum's pip install pytest requests (Sessions 32, 42) become more reproducible because of it?

requirements.txt is a plain list of package==version pairs. Anyone who clones the project can run pip install -r requirements.txt and get the exact same dependency versions you had โ€” without requirements.txt, "pip install pytest requests" (as taught in earlier sessions) gets whatever the LATEST versions happen to be at install time, which may differ machine to machine.
Question 4 of 5

You clone the Country Explorer project on a new machine and want the exact same dependencies. What is the correct sequence, given a requirements.txt already exists?

Create a fresh, isolated virtual environment first, activate it (so pip installs INTO it, not globally), then install from the requirements file. This is the standard, portable Python project setup sequence.
Question 5 of 5

At a high level, what is pyproject.toml for, as opposed to requirements.txt?

requirements.txt is about reproducing a working ENVIRONMENT for running a project. pyproject.toml is about describing a project as an installable, distributable PACKAGE โ€” the modern standard for anything meant to be published (e.g. to PyPI) or installed via pip install your-package-name.

3. The Concept โ€” Isolating and Sharing a Python Project

GLOBAL PYTHONshared,conflict-proneVENV PER PROJECTisolated,conflict-free

Each project gets its own isolated interpreter and packages โ€” no more conflicts between projects sharing one global Python.

The problem: one global Python, many conflicting needs

Every pip install across this curriculum (Session 32's pytest, Session 42's requests) quietly assumed a single, shared Python installation. In reality, different projects often need different, conflicting versions of the same package โ€” impossible to satisfy with one global set of installed packages.

Creating and activating a virtual environment

venv, built into Python, creates a self-contained directory with its own interpreter and package list โ€” completely isolated from the global installation and from any other project's environment.

# Create a virtual environment in a folder called "env"
# python -m venv env

# Activate it (varies by OS)
# macOS/Linux: source env/bin/activate
# Windows:     env\Scripts\activate

# Once activated, your prompt shows (env) โ€” pip now installs HERE, not globally
# pip install pytest requests

# Deactivate when done
# deactivate

Freezing and restoring dependencies

pip freeze lists every installed package and its exact version. Saving that to requirements.txt lets anyone recreate an identical environment โ€” turning "it works on my machine" into "run these two commands and it works on yours too."

# Inside an activated venv, after installing what you need:
# pip freeze > requirements.txt

# requirements.txt now looks something like:
# pytest==8.3.4
# requests==2.32.3

# On a fresh machine, to reproduce the exact same environment:
# python -m venv env
# source env/bin/activate   (or env\Scripts\activate on Windows)
# pip install -r requirements.txt

pyproject.toml โ€” describing a distributable package

requirements.txt reproduces an environment for RUNNING a project. pyproject.toml is the modern standard for describing a project as an installable PACKAGE โ€” its name, version, and dependencies โ€” used when you want something installable via pip install your-package-name, not just runnable locally.

# pyproject.toml โ€” a minimal example, for context only (not built in this lab)
# [project]
# name = "country-explorer"
# version = "1.0.0"
# dependencies = [
#     "requests>=2.32",
# ]
The curriculum, complete: This is the final session of Python Fundamentals, core and bonus layers alike. You now have the language fundamentals, the object-oriented and testing discipline, the architectural judgment, and the practical project-setup knowledge to build and share a real Python project.

4. Lab

Lab objective: Create a virtual environment for the Country Explorer project, install its real dependencies, and produce a requirements.txt that could recreate the setup on any machine.

What you will build

A virtual environment and a requirements.txt file.

Step-by-step instructions

1

Create a virtual environment for the project

Run this in your project's root folder.

# python -m venv env
2

Activate it and confirm you are inside it

Your terminal prompt should now show (env) at the start.

# source env/bin/activate     (macOS/Linux)
# env\Scripts\activate          (Windows)
3

Install the project's real dependencies

These are the two packages this whole curriculum actually used, in Sessions 32 and 42.

# pip install pytest requests
4

Freeze the exact versions to requirements.txt

# pip freeze > requirements.txt
# cat requirements.txt   (or open it in an editor)
5

Simulate a fresh machine: deactivate, then reproduce the environment from scratch

Delete the env folder, then rebuild it purely from requirements.txt, proving the file is sufficient on its own.

# deactivate
# rm -rf env                       (macOS/Linux)   or   rmdir /s env   (Windows)
# python -m venv env
# source env/bin/activate            (or env\Scripts\activate)
# pip install -r requirements.txt
# pip list                            # confirm pytest and requests are both back

5. Expected Files Changed

FileActionWhy
env/ Created An isolated virtual environment for this project (not committed to git).
requirements.txt Created The exact, reproducible list of the project's dependencies.
docs/sessions/session-47/index.html Created This session document โ€” the final session of the curriculum.
If you find yourself editing any other file, stop. This session touches exactly 3 files.

6. Commit Checkpoint

Once the lab is complete and you can explain every line, make this exact commit:

git add requirements.txt docs/sessions/session-47/index.html
git commit -m "session-47: isolate dependencies with venv and freeze them to requirements.txt"
Do not commit until you can answer out loud: "Why does the env/ folder itself get excluded from git (via .gitignore), while requirements.txt does get committed?"

7. Code Review Checklist

Go through your code line by line and check each item:


8. Post-Coding Quiz

Same 5 questions. Take it again now that you have written and run the code. You need 4/5 to mark this session complete.

Question 1 of 5

Two different projects on the same computer need different, conflicting versions of the requests library. What problem does this create if both use the same global Python installation?

A global Python installation has exactly ONE set of installed packages, shared by everything on that machine. Two projects needing different versions of the same library genuinely conflict โ€” this is exactly the problem virtual environments solve.
Question 2 of 5

What does a virtual environment (created with python -m venv env) actually provide?

A virtual environment is a self-contained directory holding its own Python interpreter (or a link to one) and its own site-packages โ€” installing something into it has zero effect on the global installation or any other project's environment.
Question 3 of 5

What does pip freeze > requirements.txt do, and why does the whole curriculum's pip install pytest requests (Sessions 32, 42) become more reproducible because of it?

requirements.txt is a plain list of package==version pairs. Anyone who clones the project can run pip install -r requirements.txt and get the exact same dependency versions you had โ€” without requirements.txt, "pip install pytest requests" (as taught in earlier sessions) gets whatever the LATEST versions happen to be at install time, which may differ machine to machine.
Question 4 of 5

You clone the Country Explorer project on a new machine and want the exact same dependencies. What is the correct sequence, given a requirements.txt already exists?

Create a fresh, isolated virtual environment first, activate it (so pip installs INTO it, not globally), then install from the requirements file. This is the standard, portable Python project setup sequence.
Question 5 of 5

At a high level, what is pyproject.toml for, as opposed to requirements.txt?

requirements.txt is about reproducing a working ENVIRONMENT for running a project. pyproject.toml is about describing a project as an installable, distributable PACKAGE โ€” the modern standard for anything meant to be published (e.g. to PyPI) or installed via pip install your-package-name.

9. Reflection Questions

Think through these after the post-quiz. No right answer โ€” they are for discussion.

  1. Why should the env/ folder itself never be committed to git, while requirements.txt should be? What is the difference in what each one represents?
  2. What would have gone wrong across Sessions 32-44 if two different learners' machines had silently different installed versions of pytest or requests?
  3. When would you actually need a full pyproject.toml instead of just a requirements.txt? Think about the difference between "running a project" and "publishing a package."
  4. Looking back across all 47 sessions, which single practice from this curriculum do you think will matter most the next time you start a brand-new Python project from scratch?

10. What Breaks If This Knowledge Is Missing?

  • "Works on my machine" bugs: Without virtual environments and requirements.txt, a project silently depends on whatever happens to be globally installed on one specific machine โ€” the single most common reason a project runs for its author but fails for anyone else.
  • Dependency conflicts between projects: Without isolation, working on two Python projects with different version needs on the same machine eventually forces you to break one to fix the other โ€” venv is what prevents this entirely.

11. What We Learned

Python concept mastered: Isolating project dependencies with venv, reproducing environments with pip freeze / requirements.txt, and the distinction between an environment file and a distributable package's pyproject.toml.

Unlocks: You can now set up, isolate, and share any real Python project so it runs identically on any machine โ€” the last practical skill standing between "a script that works for me" and a genuinely shareable project.

This is the final session. The curriculum, core and bonus layers alike, is complete.