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:
C Extensions
Ignore compiled C extension files:
C Extensions
Ignore compiled C extension files:
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:
Installer Logs
Logs generated by Python package managers:
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:
Framework-Specific Files
Django
Flask
Scrapy
Docuementation
Build Systems
PyBuilder
Jupyter Notebooks
Environment Files
Editor Specific Files
Vim & Neovim
To avoid temporary files created by vim and neovim
Spdyder
Rople
Type Checkers
Ignore caches and configuration filse for type checkers:
Cython
IDE-Specific Files
PyCharm
For PyCharm users, a separate template is available, but if needed:
Miscellaneous
PyPI Configuration
MkDocs
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!