Lesson 19: Packaging, Virtual Environments and Git
A program that only runs on your computer is a draft. To share it, you need two things: a way to describe what it needs, and a way to track changes as it grows. The first is packaging. A Python project is usually described by a pyproject.toml file, which names the project, its version and its dependencies, and a virtual environment keeps those dependencies isolated from every other project on your machine. The second is Git, the version-control system that records every change you make, lets you undo mistakes, and makes collaboration possible.
A virtual environment is a private copy of Python with its own installed packages. You create one with python -m venv .venv, activate it, and then pip install puts packages inside it — never in the system Python, where they could clash with other projects. The habit is worth building on day one: one project, one environment, and a requirements.txt or pyproject.toml that lists exactly what it needs. Anyone who clones your project can then recreate the same environment with one command.
$ python -m venv .venv
$ source .venv/bin/activate # on Windows: .venv\Scripts\activate
(.venv) $ pip install requests
(.venv) $ pip freeze > requirements.txt
Git in ten minutes a day
Git tracks your project as a series of commits — snapshots of the whole project at a moment in time, each with a message explaining what changed and why. The daily workflow is short: git status to see what changed, git add to stage the changes you want, git commit -m "..." to record them, and git log to review the history. When something breaks, git diff shows exactly what changed, and git checkout can take you back to the last working version. That safety net is what lets you experiment boldly: nothing is ever truly lost while Git is watching.
Two rules will save you most Git pain. First, commit small and often, with messages that explain why, not just what — "fix off-by-one in word count" beats "stuff". Second, never commit secrets or generated files: keep a .gitignore file that excludes your virtual environment, your __pycache__ folders, and any file holding a password or API key. A repository is a public record of your work; treat it as if the whole world will read it, because one day it might. With packaging and Git in place, your capstone tool is ready to be shared — which is exactly what the final lesson does.
Going further
Once a project has a pyproject.toml, you can install it in editable mode with pip install -e ., which links the installed package to your source files instead of copying them. Changes you make to the code take effect immediately, without reinstalling, which makes development far smoother. The -e flag is one of those small habits that separates a comfortable workflow from a frustrating one, and it is worth adopting from your first packaged project onwards.
Version numbers also follow a convention worth knowing: semantic versioning. A version such as 2.4.1 has three parts — major, minor and patch. The major number changes when you break compatibility, the minor when you add a feature that stays compatible, and the patch when you fix a bug. Users reading your version number can tell at a glance whether an update is safe to install. It is a small discipline, but it is how the whole Python ecosystem communicates about change, and your own projects will be easier to trust if they follow it.
A good first repository to practise with is a tiny one of your own: a single Python file, a README, a .gitignore that excludes the virtual environment, and a handful of commits with clear messages. Then deliberately make a mistake — delete a function, commit, and realise you need it back — and recover it with git log and git checkout. Doing this once, in a repository where nothing matters, teaches you the recovery commands far better than reading about them. Version control is a safety net, and like any safety net, you should test it before you need it.
Key Terms
- Packaging
- Describing a project — its name, version and dependencies — so others can install and run it.
- pyproject.toml
- The standard file describing a Python project and its dependencies.
- Virtual environment
- An isolated Python installation for one project, created with python -m venv.
- pip
- Python's package installer; pip install adds a package to the active environment.
- Git
- A version-control system that records a project's history as a series of commits.
- Commit
- A snapshot of the project at one moment, with a message explaining the change.
- .gitignore
- A file listing paths Git should never track, such as virtual environments and secrets.
- requirements.txt
- A file listing the exact packages a project needs, produced by pip freeze.
Discussion Questions
- Why is installing packages into the system Python a bad idea? What goes wrong when two projects need different versions of the same package?
- What makes a good commit message? Compare "fix bug" with "fix off-by-one in word count when file ends without newline".
- What should never be committed to Git, and why? Think beyond secrets — what about generated files?
Practice Questions
- What command creates a virtual environment named
.venv? - After activating the environment, where does
pip install requestsput the package? - What does
pip freeze > requirements.txtdo? - What is the difference between
git addandgit commit? - Why should a
.gitignoreexclude the.venvfolder?
Show answers
python -m venv .venv. It creates a private Python installation with its own packages.- Inside the virtual environment, not the system Python. That isolation is the whole point: projects cannot clash over package versions.
- It writes the list of installed packages and versions into requirements.txt. Anyone can recreate the same environment from that file.
- add stages changes; commit records the staged changes as a snapshot. Staging lets you choose exactly which changes belong in the next commit.
- It is large, machine-specific, and recreatable from requirements.txt. Committing it would bloat the repository and could break on another computer.