$devtoolkit.sh/examples/config/gitignore-python

.gitignore for a Python Project

Python projects generate bytecode files, virtual environment directories, and test coverage data that should never be committed. This .gitignore covers venv, .venv, and virtualenv directories, all .pyc bytecode files, pytest and coverage caches, distribution packages, and environment files. The gitignore generator can merge this with IDE-specific patterns for PyCharm or VS Code. Always add your virtual environment directory name explicitly in case you use a non-standard name like env or .env.

Example
# Virtual environments
venv/
.venv/
env/
ENV/

# Bytecode
__pycache__/
*.py[cod]
*.pyo

# Distribution
dist/
build/
*.egg-info/
*.egg

# Testing
.pytest_cache/
.coverage
htmlcov/
.tox/

# Environment
.env
.env.local

# Jupyter
.ipynb_checkpoints/

# OS
.DS_Store
[ open in .gitignore Generator → ]

FAQ

Why should I ignore __pycache__ directories?
__pycache__ contains compiled bytecode files (.pyc) that are generated automatically. They are machine-specific, regenerated on every run, and add unnecessary noise to git diffs.
Should I commit my virtual environment?
No. Virtual environments are large, platform-specific, and fully reproducible from requirements.txt or pyproject.toml. Commit the requirements file and regenerate the venv on each machine.
How do I ignore all .pyc files regardless of directory?
Use the pattern **/*.pyc or simply *.pyc. The double-asterisk matches any depth of subdirectory, ensuring bytecode files in nested packages are also excluded.

Related Examples

/examples/config/gitignore-pythonv1.0.0