DEV Community

Hastycode Andreh
Hastycode Andreh

Posted on

Resolving Missing `_sqlite3` Module in Python on Ubuntu

If you encounter the error ModuleNotFoundError: No module named '_sqlite3' while running Python scripts, it means the SQLite module required by the sqlite3 library is not installed. This is common when Python is built from source without the required SQLite development libraries. Here's how to resolve the issue:


1. Install SQLite Development Libraries

Ensure your system has the necessary SQLite development libraries by running the following commands in your terminal:

sudo apt update  
sudo apt install libsqlite3-dev  
Enter fullscreen mode Exit fullscreen mode

This installs the libsqlite3-dev package, which provides the header files and libraries needed for Python's SQLite support.


2. Rebuild Python with pyenv

If you are using pyenv to manage Python versions, you need to reinstall the affected version to include SQLite support. Use this command:

pyenv install --force 3.8.13  
Enter fullscreen mode Exit fullscreen mode

This forces pyenv to rebuild Python version 3.8.13, ensuring SQLite support is enabled.


3. Verify Python Installation

After rebuilding Python, you can confirm that SQLite support is working correctly. Run the following command:

python -c "import sqlite3; print('SQLite is working')"
Enter fullscreen mode Exit fullscreen mode

If you see SQLite is working in the output, the issue has been resolved.


Additional Tip

You can check your installed Python versions using:

pyenv versions  
Enter fullscreen mode Exit fullscreen mode

Rebuild any version of Python where SQLite support might be missing.

By following these steps, you'll ensure that your Python environment is fully equipped to handle SQLite operations.

Top comments (0)