.gitignore for Python Projects
Essential .gitignore patterns for Python projects. Covers virtualenvs, __pycache__, .pyc files, dist folders, and Python-specific packaging artifacts.
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 viapip install -r requirements.txt.*.egg-info/anddist/— Package distribution artifacts created bysetuptoolsorpip install -e .. These are build outputs, not source code..eggs/andbuild/— Additional packaging directories used duringpython setup.py buildor wheel creation.*.pyo— Optimized bytecode files generated withpython -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.