.gitignore for Python Projects

Essential .gitignore patterns for Python projects. Covers virtualenvs, __pycache__, .pyc files, dist folders, and Python-specific packaging artifacts.

Language

Detailed Explanation

Python generates several categories of files that should never be committed. A correct .gitignore prevents repository pollution and avoids shipping compiled bytecode or virtual environment binaries.

Core patterns every Python project needs:

  • __pycache__/ and *.pyc — Python compiles source files to bytecode (.pyc) and stores them in __pycache__/ directories. These are platform-specific and regenerated automatically on import. The pattern **/__pycache__/ catches nested occurrences.
  • venv/, env/, .venv/ — Virtual environment directories. These contain a full copy of the Python interpreter and installed packages, often totaling hundreds of megabytes. Collaborators recreate them via pip install -r requirements.txt.
  • *.egg-info/ and dist/ — Package distribution artifacts created by setuptools or pip install -e .. These are build outputs, not source code.
  • .eggs/ and build/ — Additional packaging directories used during python setup.py build or wheel creation.
  • *.pyo — Optimized bytecode files generated with python -O.
  • .mypy_cache/ and .pytest_cache/ — Caches from type checking and testing tools. These are transient and machine-specific.
  • .tox/ — Tox testing automation directories containing isolated test environments.
  • *.ipynb_checkpoints/ — Jupyter Notebook auto-save checkpoints that create noise in diffs.

Important: Always commit requirements.txt, Pipfile.lock, or poetry.lock. These lockfiles are the blueprint for reproducing your environment. Without them, teammates may install incompatible package versions.

For Django projects, also add db.sqlite3, media/, and staticfiles/. For data science work, consider ignoring large dataset files (*.csv, *.parquet) and tracking them with Git LFS instead.

Use Case

A data science team setting up a shared Django and scikit-learn project needs to ignore virtualenvs, bytecode caches, and Jupyter checkpoints to keep their repo clean.

Try It — .gitignore Generator

Open full tool