Sub topic 11
Keeping environments out of version control and rebuilding them
This chapter assumes you can create and activate a virtual environment, install packages with python -m pip, and write a requirements.txt with pip freeze. It covers where to keep the environment, what to commit, and how to rebuild it.
Why the environment stays out of Git
A virtual environment is a build product, not source code. It has three properties that make it a poor fit for version control:
- It is large. A modest project can have thousands of files in
site-packages. - It is tied to one machine. The
pyvenv.cfgfile and the scripts inbinorScriptscontain absolute paths to the interpreter that created them. - It can be rebuilt. The requirements file holds the information needed to recreate it.
If you commit an environment and a teammate clones it on another operating system or Python version, it will not work. It also fills the repository history with binary files.
A conventional project layout
Put the environment in the project directory and name it .venv. Many editors and tools look for that name and detect it automatically.
myproject/
├── .git/
├── .gitignore
├── .venv/ # not committed
├── requirements.txt # committed
├── pyproject.toml # committed
└── src/Add the environment and Python's cache files to .gitignore:
.venv/
__pycache__/If you also use other names, such as .venv-new from earlier examples, add them to the file or use a pattern like .venv*/.
What to commit instead
Commit the files that describe the environment, not the environment itself:
| File | Purpose | Commit? |
|---|---|---|
| `requirements.txt` | Packages and versions to install | Yes |
| `constraints.txt` | Version limits for the resolver | Yes, if you use one |
| `pyproject.toml` | Project metadata and dependencies | Yes |
| `.venv/` | The installed environment | No |
The rebuild cycle
Because the environment is disposable, you can delete it and recreate it whenever it is in doubt. On Linux or macOS:
deactivate
rm -rf .venv
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python -m pip checkThe final pip check confirms that no installed package has a missing or incompatible dependency. On Windows PowerShell, replace the deletion with Remove-Item -Recurse -Force .venv and activate with .venv\Scripts\Activate.ps1.
Keeping the requirements file accurate
Rebuilding only works if requirements.txt matches what the project needs. Two habits help:
- Regenerate the file after you intentionally change dependencies:
python -m pip freeze > requirements.txt. Review the diff before committing. - Test the file occasionally by installing it into a fresh environment, as in the earlier
.venv-newexample. If the install succeeds and your tests pass, the file is complete.
Remember that pip freeze lists every installed package, including indirect dependencies. That gives exact repeatability, but it makes the file longer and harder to edit by hand. Some teams keep a short file of direct dependencies and use a constraints file for exact versions.
Common mistakes
- Deleting the environment while it is active. Run
deactivatefirst, or your shell prompt andPATHwill still refer to a directory that no longer exists. - Moving or renaming
.venv. The scripts contain absolute paths, so the environment breaks. Recreate it in the new location. - Forgetting to freeze after installing. A package you installed by hand will be missing after the next rebuild.
Exercise
A teammate clones your repository and finds that the .venv directory is missing. They ask you to commit it. What do you tell them to do instead, and which two files make that possible?
Show answer
They should create their own environment with python3 -m venv .venv, activate it, and run python -m pip install -r requirements.txt. The two files are .gitignore, which keeps .venv/ out of the repository, and requirements.txt, which records the packages and versions to install.
Exercise
You added .venv/ to .gitignore, but git status still shows files inside it as tracked. Why, and how do you fix it?
Show answer
Git ignores only untracked files, and these were committed earlier. Run git rm -r --cached .venv to stop tracking them without deleting them from disk, then commit.