.gitignore for a Python Project
Python's development workflow generates numerous files that are machine-specific, regenerated automatically, or contain sensitive configuration — none of which belong in version control. Understanding what each category of ignored files represents helps you make informed decisions about what to include and exclude as your project grows beyond this standard baseline. Virtual environment directories (venv/, .venv/, env/, ENV/) are the most important entries. A Python virtual environment contains a complete Python installation and all installed packages — often hundreds of megabytes and tens of thousands of files. It's fully regenerated from requirements.txt or pyproject.toml, is operating-system specific (Windows .dlls and Unix .so files can't be mixed), and contains absolute paths to the Python installation that would be wrong on other machines. Always ignore virtual environments regardless of what you named yours. The name of your virtual environment can vary: some developers use venv (the standard name from python -m venv venv), others use .venv (hidden directory, common in VS Code Python extension), env (older convention), or project-specific names. This .gitignore covers the most common conventions, but if you use a different name, add it explicitly. A virtual environment accidentally committed to git will contaminate git history for the life of the repository. __pycache__/ directories and *.pyc files are Python's bytecode cache. Python compiles source files to bytecode (.pyc) and stores them in __pycache__/ for performance. This cache is: machine-specific (not compatible across different Python versions), automatically regenerated on startup, not needed for deployment, and adds noise to git diffs. The ** prefix in **/__pycache__/ matches at any directory depth. Distribution artifacts (dist/, build/, *.egg-info/) are generated by setuptools when you build a Python package for distribution. They're generated by python -m build and belong in package registry uploads (PyPI), not in version control. Including them creates confusion about which version is authoritative. Test tooling artifacts: .pytest_cache/ is pytest's cache of test results and fixtures. .coverage is the coverage.py data file containing line-by-line execution data from your test run. htmlcov/ is the generated HTML coverage report. .tox/ contains tox's test environments. All of these are generated fresh on each test run and are meaningless outside the developer's current context. Jupyter Notebook checkpoints: .ipynb_checkpoints/ stores auto-saved checkpoint versions of Jupyter notebooks. These accumulate silently as you work and would clutter git history with intermediate notebook states that aren't meaningful. Tips: run git ls-files --ignored --exclude-standard to see which files in your repository are currently being ignored. If you discover files that should have been ignored but were committed before the .gitignore was set up, use git rm --cached to stop tracking them without deleting them from your filesystem.
# Virtual environments venv/ .venv/ env/ ENV/ # Bytecode __pycache__/ *.py[cod] *.pyo # Distribution dist/ build/ *.egg-info/ *.egg # Testing .pytest_cache/ .coverage htmlcov/ .tox/ # Environment .env .env.local # Jupyter .ipynb_checkpoints/ # OS .DS_Store
FAQ
- Why should I ignore __pycache__ directories?
- __pycache__ contains compiled bytecode files (.pyc) that are generated automatically. They are machine-specific, regenerated on every run, and add unnecessary noise to git diffs.
- Should I commit my virtual environment?
- No. Virtual environments are large, platform-specific, and fully reproducible from requirements.txt or pyproject.toml. Commit the requirements file and regenerate the venv on each machine.
- How do I ignore all .pyc files regardless of directory?
- Use the pattern **/*.pyc or simply *.pyc. The double-asterisk matches any depth of subdirectory, ensuring bytecode files in nested packages are also excluded.
Related Examples
A .gitignore file is one of the first files you should create in any new project...
Dockerfile for a Python ApplicationBuilding a production Python Docker image requires understanding several Python-...
Manage a .env Environment FileEnvironment files are the standard mechanism for injecting configuration and sec...