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.
| 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 |
| 11 | build/ |
| 12 | develop-eggs/ |
| 13 | dist/ |
| 14 | downloads/ |
| 15 | eggs/ |
| 16 | .eggs/ |
| 17 | lib/ |
| 18 | lib64/ |
| 19 | parts/ |
| 20 | sdist/ |
| 21 | var/ |
| 22 | wheels/ |
| 23 | *.egg-info/ |
| 24 | .installed.cfg |
| 25 | *.egg |
| 26 | |
| 27 | # Virtual environments |
| 28 | venv/ |
| 29 | .venv/ |
| 30 | env/ |
| 31 | ENV/ |
| 32 | env.bak/ |
| 33 | venv.bak/ |
| 34 | |
| 35 | # Testing & Coverage |
| 36 | htmlcov/ |
| 37 | .tox/ |
| 38 | .nox/ |
| 39 | .coverage |
| 40 | .coverage.* |
| 41 | .cache |
| 42 | nosetests.xml |
| 43 | coverage.xml |
| 44 | *.cover |
| 45 | *.py,cover |
| 46 | .hypothesis/ |
| 47 | .pytest_cache/ |
| 48 | |
| 49 | # Type checking |
| 50 | .mypy_cache/ |
| 51 | .dmypy.json |
| 52 | dmypy.json |
| 53 | .pyre/ |
| 54 | .pytype/ |
| 55 | |
| 56 | # Environment variables |
| 57 | .env |
| 58 | .env.* |
| 59 | !.env.example |
Key Rules & Why They Are Ignored
__pycache__/ & *.pycCompiled 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
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`.