git

.gitignore for Python

Comprehensive .gitignore for Python projects covering virtual environments, bytecode, packaging, and common IDE files.

Overview

A comprehensive .gitignore for Python projects including Django, Flask, FastAPI, and data science workflows. Covers virtual environments, compiled bytecode, packaging artifacts, testing output, and IDE configurations.

Configuration

# .gitignore

# ── Byte-compiled / Optimized / DLL files ──
__pycache__/                   # Python bytecode cache directories
*.py[cod]                      # .pyc, .pyo, .pyd files
*$py.class                     # Python class files

# ── C Extensions ──
*.so                           # Shared object files (Linux)

# ── Virtual Environments ──
venv/                          # Standard virtual environment
.venv/                         # Dotfile virtual environment
env/                           # Alternative env name
ENV/                           # Alternative env name (uppercase)
.conda/                        # Conda environment
*.egg-info/                    # Package metadata

# ── Distribution / Packaging ──
dist/                          # Built distribution packages
build/                         # Build artifacts
eggs/                          # Egg packages
*.egg                          # Egg files
*.whl                          # Wheel packages
sdist/                         # Source distributions
wheels/                        # Wheel build output

# ── Installer Logs ──
pip-log.txt                    # pip log file
pip-delete-this-directory.txt  # pip cleanup marker

# ── Testing ──
.pytest_cache/                 # pytest cache directory
.coverage                      # Coverage.py data file
.coverage.*                    # Coverage.py parallel data
htmlcov/                       # Coverage HTML reports
coverage.xml                   # Coverage XML report
*.cover                        # Coverage data files
.tox/                          # tox environments
.nox/                          # nox environments
nosetests.xml                  # Nose test results
reports/                       # Test reports directory

# ── Type Checking ──
.mypy_cache/                   # mypy cache
.pytype/                       # pytype cache
.pyre/                         # Pyre cache

# ── Environment Variables ──
.env                           # Environment variables
.env.local                     # Local env overrides
.env.*.local                   # Env-specific local files

# ── Django ──
*.log                          # Log files
local_settings.py              # Local Django settings
db.sqlite3                     # SQLite database
db.sqlite3-journal             # SQLite journal
media/                         # User-uploaded media files
staticfiles/                   # Collected static files

# ── Flask ──
instance/                      # Flask instance folder
.webassets-cache               # Flask-Assets cache

# ── Jupyter / Data Science ──
.ipynb_checkpoints/            # Jupyter notebook checkpoints
*.ipynb_checkpoints            # Alternative checkpoint pattern
.jupyter/                      # Jupyter config

# ── Scrapy ──
.scrapy                        # Scrapy project data

# ── Celery ──
celerybeat-schedule            # Celery Beat schedule file
celerybeat.pid                 # Celery Beat PID file

# ── Documentation ──
docs/_build/                   # Sphinx build output
site/                          # MkDocs build output

# ── OS Files ──
.DS_Store                      # macOS metadata
Thumbs.db                      # Windows thumbnails
*~                             # Backup files

# ── IDE / Editor ──
.idea/                         # JetBrains/PyCharm
.vscode/*                      # VS Code settings
!.vscode/settings.json         # Keep shared settings
!.vscode/extensions.json       # Keep recommended extensions
*.swp                          # Vim swap files
*.swo                          # Vim swap files

# ── Caches ──
.cache/                        # Generic cache
.ruff_cache/                   # Ruff linter cache

Key Options Explained

  • __pycache__/ — Python compiles modules to bytecode and stores them here. Always regenerated from source files.
  • *.py[cod] — Matches .pyc (compiled), .pyo (optimized), and .pyd (Windows DLL) files using a character class.
  • Virtual environments — Multiple patterns cover common naming conventions (venv, .venv, env). These directories contain installed packages and should be recreated from requirements.txt.
  • *.egg-info/ — Generated when running pip install -e . for editable installs. Contains package metadata that is regenerated on install.
  • db.sqlite3 — SQLite databases are binary files that cause merge conflicts and differ between environments.

Common Modifications

  • Poetry: Add .venv/ if using poetry config virtualenvs.in-project true (already covered above).
  • Data files: Add data/ or *.csv / *.parquet if your project downloads large datasets that shouldn’t be versioned.
  • ML models: Add models/ or *.pkl / *.h5 / *.pt for trained model files. Use Git LFS for large binary files.
  • Alembic: Keep alembic/versions/*.py tracked (migration files) but ignore alembic.ini if it contains database credentials.
  • Pre-commit config: Keep .pre-commit-config.yaml tracked for consistent team hooks.