Python
SQLite3
Error Fixing
Troubleshooting
Programming

No module named _sqlite3

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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 pyenv without 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:

bash
python3 -c "import sqlite3; print(sqlite3.sqlite_version)"

If that fails with _sqlite3, test the extension directly:

bash
python3 -c "import _sqlite3"

You can also confirm which interpreter you are actually using:

bash
which python3
python3 -V

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:

bash
sudo apt-get update
sudo apt-get install libsqlite3-dev

On Fedora, RHEL, or similar systems, the package is commonly named sqlite-devel:

bash
sudo dnf install 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.

bash
pyenv uninstall 3.12.2
pyenv install 3.12.2

Or force a rebuild:

bash
pyenv install --force 3.12.2

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.

bash
brew install sqlite
bash
1export LDFLAGS="-L$(brew --prefix sqlite)/lib"
2export CPPFLAGS="-I$(brew --prefix sqlite)/include"
3export PKG_CONFIG_PATH="$(brew --prefix sqlite)/lib/pkgconfig"
4pyenv install --force 3.12.2

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:

python
1import sqlite3
2
3print(sqlite3.sqlite_version)
4
5conn = sqlite3.connect(":memory:")
6cur = conn.cursor()
7cur.execute("CREATE TABLE demo (id INTEGER, name TEXT)")
8cur.execute("INSERT INTO demo VALUES (?, ?)", (1, "hello"))
9cur.execute("SELECT * FROM demo")
10print(cur.fetchone())
11conn.close()

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 sqlite3 is 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

  • '_sqlite3 is 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.
  • 'pyenv users must reinstall the affected Python version after fixing dependencies.'
  • Virtual environments inherit the problem from the base interpreter, so fix the interpreter itself.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.