Git β€’ Languages & Frameworks

Python .gitignore Template

Python generates compiled bytecode files (*.pyc) and ephemeral virtual environment folders. This template keeps your Python repository clean across pip, poetry, conda, pytest, and mypy.

.gitignore(59 lines)
1# Byte-compiled / optimized / DLL files
2__pycache__/
3*.py[cod]
4*$py.class
5
6# C extensions
7*.so
8
9# Distribution / packaging
10.Python
11build/
12develop-eggs/
13dist/
14downloads/
15eggs/
16.eggs/
17lib/
18lib64/
19parts/
20sdist/
21var/
22wheels/
23*.egg-info/
24.installed.cfg
25*.egg
26
27# Virtual environments
28venv/
29.venv/
30env/
31ENV/
32env.bak/
33venv.bak/
34
35# Testing & Coverage
36htmlcov/
37.tox/
38.nox/
39.coverage
40.coverage.*
41.cache
42nosetests.xml
43coverage.xml
44*.cover
45*.py,cover
46.hypothesis/
47.pytest_cache/
48
49# Type checking
50.mypy_cache/
51.dmypy.json
52dmypy.json
53.pyre/
54.pytype/
55
56# Environment variables
57.env
58.env.*
59!.env.example

Key Rules & Why They Are Ignored

__pycache__/ & *.pyc

Compiled Python bytecode files automatically generated by the CPython interpreter.

venv/ & .venv/

Local virtual environment containing downloaded pip wheels and packages.

*.egg-info/ & dist/

Packaging build outputs generated by setuptools, flit, or poetry.

.pytest_cache/

Intermediate test cache generated by running pytest.

Already Committed Ignored Files? Purge from Git Cache

Adding a pattern to .gitignore does NOT delete files that were already tracked in previous commits. Run this command to remove them from tracking without deleting your local copies:

git rm -r --cached . && git add . && git commit -m "Untrack ignored files"

Frequently Asked Questions About Python .gitignore

Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

Never. Virtual environments contain platform-specific compiled binaries that will not work on other operating systems or CI servers. Instead, commit `requirements.txt` or `pyproject.toml`.