No module named _sqlite3
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The error ModuleNotFoundError: No module named '_sqlite3' usually means your Python interpreter was built without SQLite support. The public sqlite3 module depends on a compiled C extension named _sqlite3, so if that extension was skipped during the Python build, every environment created from that interpreter will inherit the problem.
Why It Happens
Python does not ship _sqlite3 as a pure Python file. It is a compiled extension module that is built only if the SQLite library and development headers are available when Python itself is compiled.
That means the error is most common when:
- Python was built from source manually
- Python was installed with
pyenvwithout SQLite development packages present - a custom Docker or CI image built Python in a minimal environment
By contrast, system package manager builds and most Conda builds usually include SQLite support already.
Confirm the Problem
A quick check is:
If that fails with _sqlite3, test the extension directly:
You can also confirm which interpreter you are actually using:
This matters because many machines have several Python installations, and the broken one is often not the one you think you are running.
Linux Fix: Install Development Headers Before Rebuilding Python
On Debian or Ubuntu, install the SQLite development package before rebuilding Python:
On Fedora, RHEL, or similar systems, the package is commonly named sqlite-devel:
Then rebuild or reinstall the Python version that failed. Merely installing the package afterward is not enough, because the interpreter has already been compiled without _sqlite3.
pyenv Fix
pyenv is one of the most common places this problem shows up because it compiles Python locally. After installing the SQLite development package, reinstall the affected version.
Or force a rebuild:
If you skip the reinstall step, the old interpreter remains unchanged and the error persists.
macOS Fix
On macOS, install SQLite first, often through Homebrew, then rebuild Python with the right flags visible.
The exact environment variables are less important than the principle: the Python build process must be able to find the SQLite headers and libraries.
Virtual Environments Do Not Fix a Broken Base Interpreter
A virtual environment reuses the standard library and compiled modules from its base interpreter. So if the base Python lacks _sqlite3, every venv created from it will also lack _sqlite3.
That is why the fix belongs at the interpreter-installation level, not the venv level.
Verify After Rebuilding
Once Python is rebuilt correctly, verify both import and basic functionality:
If this works, the extension is present and usable.
Common Pitfalls
- Installing SQLite development packages after Python is already built and forgetting to rebuild Python.
- Recreating virtual environments instead of fixing the broken base interpreter.
- Debugging the wrong interpreter when multiple Python installations exist.
- Assuming
pip install sqlite3is the answer, even though this is not a normal pip-only dependency problem. - Forgetting to expose Homebrew SQLite paths when building Python on macOS.
Summary
- '
_sqlite3is a compiled Python extension, not a pure Python module.' - The error usually means Python was built without SQLite development libraries available.
- Install the OS-level SQLite development package first, then rebuild or reinstall Python.
- '
pyenvusers must reinstall the affected Python version after fixing dependencies.' - Virtual environments inherit the problem from the base interpreter, so fix the interpreter itself.

