How to Fix the 'ModuleNotFoundError: No module named pre_commit' Error
Getting the ModuleNotFoundError: No module named 'pre_commit' error is a classic "Lost in Translation" moment between your terminal and your Python environment. You know you want to commit code, and Git knows it needs to run a hook, but the Python interpreter looking for the pre_commit package is coming up empty-handed.
Here is the straightforward guide to finding that missing module and getting your hooks back on track.
Why is this happening?โ
Even if you swear you installed it five minutes ago, this error usually stems from one of three "Environment Mismatches":
- The Virtualenv Void: You installed
pre-commitin your global Python, but you're currently working inside a virtual environment (or vice-versa). - The Version Confusion: You have both Python 3.x and Python 2.x (or multiple 3.x versions), and the
pre-commitscript is pointing to the wrong one. - The PATH Problem: The directory where Python installs scripts isn't in your system's PATH, so your shell can't "see" the module.
The Fixes: From Easiest to Most Robustโ
1. The "Just Install It" Fixโ
The most likely culprit is that it simply isn't in your current environment. Run this first:
pip install pre-commit
Note: If you are on a Mac/Linux and don't use virtualenvs, you might need pip3 install pre-commit.
2. The "Interpreter Match" Fixโ
Sometimes the pre-commit executable is found, but it's trying to use a Python version that doesn't have the library. You can force it to run using your current Python module logic:
python -m pre_commit --version
If this works but just typing pre-commit doesn't, your PATH is likely pointed to a different Python version's script folder.
3. Re-installing the Hooksโ
If you recently changed Python versions or moved your project folder, the hooks inside your .git folder might be pointing to a "ghost" interpreter. You need to wipe them and start fresh:
# 1. Uninstall the current hooks
pre-commit uninstall
# 2. Re-install them using your current (working) environment
pre-commit install
Troubleshooting Command Tableโ
| If you see... | Try this command | Why? |
|---|---|---|
| Command not found | pip install pre-commit | The tool isn't installed at all. |
| No module named... | python -m pip install pre-commit | Ensures you're installing to the active Python. |
| Hook fails on commit | pre-commit clean | Clears the pre-commit cache which might be corrupted. |
| Still failing? | which pre-commit | Shows you exactly which file is being executed. |
Best Practice: Use a Virtual Environmentโ
To avoid this error forever, stop installing tools globally. Always use a virtual environment (venv or conda) for your projects.
When you use a venv, pre-commit creates its own isolated environments for each hook (like flake8 or black), which prevents "Dependency Hell" where one tool's requirements break another's.
Sources & Technical Refsโ
- [1.1] Pre-commit.com: Installation Guide - Official docs on the supported installation methods.
- [2.1] Python.org: Installing Python Modules - Understanding how
pipmaps to specific interpreters. - [3.1] Real Python: Python Virtual Environments - Why isolation prevents
ModuleNotFoundError.
