Skip to content

Gitignore for Python Projects - A Comprehensive Guide

When working on Python projects, managing your .gitignore file effectively ensures that only relevant files are tracked by version control, keeping your repository clean and manageable. This guide walks you through creating a robust .gitignore for Python projects and includes essential patterns for ignoring common temporary and environment-specific files. We'll also add configurations to avoid Vim and Neovim swap files.

Why Use a .gitignore?

A .gitignore file specifies which files and directories Git should ignore. This prevents temporary or sensitive data (such as environment files, logs, and build artifacts) from being committed to your repository. A well-configured .gitignore improves collaboration and protects sensitive information.

The Essential .gitignore for Python Projects

Byte-Compiled / Optimized / DLL Files

These files are generated automatically by Python and should not be versioned:

__pycache__/
*.py[cod]
*$py.class

C Extensions

Ignore compiled C extension files:

C Extensions

Ignore compiled C extension files:

*.so

Distribution / Packaging

These directories and files are created during packaging and distribution:

.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

PyInstaller

Files used by PyInstaller for packaging Python applications:

*.manifest
*.spec

Installer Logs

Logs generated by Python package managers:

*.manifest
*.spec

Unit Test / Coverage Reports

Files and directories created during testing:

htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

Translations

Ignore compiled translation files:

*.mo
*.pot

Framework-Specific Files

Django

*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

Flask

instance/
.webassets-cache

Scrapy

.scrapy

Docuementation

docs/_build/

Build Systems

PyBuilder

.pybuilder/
target/

Jupyter Notebooks

.ipynb_checkpoints

Environment Files

.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

Editor Specific Files

Vim & Neovim

To avoid temporary files created by vim and neovim

*.swp
*.swo
*.swn

Spdyder

.spyderproject
.spyproject

Rople

.ropeproject

Type Checkers

Ignore caches and configuration filse for type checkers:

.mypy_cache/
.dmypypy.json
dmypy.json
.pyre/
.pytype/

Cython

cython_debug/

IDE-Specific Files

PyCharm

For PyCharm users, a separate template is available, but if needed:

.idea/

Miscellaneous

PyPI Configuration

.pypirc

MkDocs

/site

Adding Your .gitignore to a Python Project

  • Create a .gitignore file in the root of your project directory.

  • Copy the template above into the file.

  • Customize it as needed based on your project’s specific requirements.

Conclusion

A thoughtfully crafted .gitignore file is a critical component of any Python project. It helps maintain a clean and efficient repository while safeguarding sensitive or irrelevant files. Use this guide as a reference to create or improve your project’s .gitignore. Happy coding!