Python .gitignore template
The Python template ignores compiled bytecode, virtual environments, packaging output and the cache directories that test and type tools leave behind.
At a glance
- Category
- Language
- Patterns in this template
- 44 rules
- Directories ignored
- 25 of the 44 rules end in a slash, so they match directories only
- Also needs an OS template
- Yes. Nothing here matches .DS_Store or Thumbs.db.
- Typical companions
- Django or Flask if you use one, plus an editor template and an OS template.
In detail
__pycache__ and the *.py[cod] pattern cover compiled bytecode. The interpreter writes it automatically the first time it imports a module, and it is both version specific and implementation specific: bytecode produced by one interpreter version will not be loaded by another. It is regenerated on demand, so a committed .pyc is pure noise that produces spurious conflicts on every branch.
The virtual environment rules matter more. A committed venv, .venv or env directory bundles hundreds of megabytes of installed packages plus scripts with the absolute path of the machine that created them baked into their shebang lines and activation scripts. It will not work anywhere else. The requirements file or project metadata is what makes the environment reproducible, and that is what belongs in the repository.
The packaging block covers build, dist, sdist, wheels, eggs, develop-eggs and *.egg-info. These are what the build backend produces on its way to a distributable archive. They are derived entirely from your sources and the project configuration, and republishing from a stale committed copy is a real way to ship the wrong code.
Testing and typing tools each keep their own cache in the tree: .pytest_cache, .tox, .nox, .hypothesis, .mypy_cache, .ruff_cache and .pytype, along with the coverage outputs .coverage, htmlcov and coverage.xml. None of it is portable and all of it is regenerated. The .env rule here is the important one for secrets, since a Python project keeps its database URL and API keys there by convention.
One rule deserves suspicion. lib/ and lib64/ exist because older packaging layouts wrote there, but the pattern is not anchored, so a project with its own source package named lib will find that package silently ignored. If your imports work locally but a fresh clone cannot find a module, this is the first rule to check.
Examples
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
pip-log.txt
pip-delete-this-directory.txt
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
*.mo
*.pot
.env
.venv
env/
venv/
ENV/
.mypy_cache/
.ruff_cache/
.pytype/44 lines exactly as the tool writes them, before any common additions you tick.
git rm --cached .env
git commit -m "stop tracking .env"
git check-ignore -v .envThe last line confirms which rule now matches it. Removing the file from the index does not remove it from history, so treat anything it held as leaked.
Things that catch people out
- The lib/ rule matches any directory called lib at any depth, including a source package you wrote. Anchor it as /lib/ or delete it if your project keeps code there.
- When a file is ignored and you cannot work out which rule did it, run git check-ignore -v path/to/file. It prints the file, the line number and the exact pattern that matched, which is far quicker than reading the file top to bottom.
- Adding .env to the ignore list does nothing if the file is already tracked. Run git rm --cached .env, commit that removal, and then rotate the credentials, because they are still in the history of every clone.
Try it: .gitignore Generator
Pick one or more templates, tick the common additions you want, and copy or download the combined .gitignore.
.gitignore Generator
LOCAL · Browser-OnlyGenerate comprehensive .gitignore files by combining templates for languages, frameworks, IDEs, and operating systems
Language
Framework
IDE
OS
Build Tool
Infrastructure
Common Additions
Preview
Select templates or common additions to generate a .gitignore file.
Related .gitignore templates
All .gitignore templates →Django .gitignore template
The Django template adds four framework rules to the usual Python ones: local settings, the SQLite database, uploaded media and collected static files.
Flask .gitignore template
The Flask template adds the instance folder to the usual Python rules, which is the framework's designated place for configuration that must not be committed.
Jupyter Notebook .gitignore template
The Jupyter template ignores autosave checkpoints and profile folders, but the real problem with notebooks in git is the output cells, which no rule fixes.
Java .gitignore template
What the Java .gitignore template ignores: compiled classes, packaged archives, crash dumps, and the negations that keep the Gradle wrapper jar tracked.
Go .gitignore template
The Go template ignores platform binaries by extension, which misses the extensionless binary go build produces on Linux and macOS. Here is what to add.
Rust .gitignore template
The Rust template ignores the target directory and leaves Cargo.lock tracked. Whether that is right depends on whether you ship a binary or a library.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS
- .env Editor & Validator Edit and validate .env files online - detect duplicates, mask secrets, generate .env.example