Fixing ModuleNotFoundError: No module named '_sqlite3'

Categories
mod_wsgi Python Sysadminning

Today, I set up Flaskr (GitHub repository), one of the Flask demo apps, so that I could poke around Flask a bit.

The only real speed bump was a ModuleNotFoundError for _sqlite3: [wsgi:info] mod_wsgi (pid=17417, process=”, application=’example.com|/blog’): Loading WSGI script ‘/home/exampledotcom/flaskr/flaskr.wsgi’. [wsgi:error] mod_wsgi (pid=17417): Target WSGI script ‘/home/exampledotcom/flaskr/flaskr.wsgi’ cannot be loaded as Python module. [wsgi:error] mod_wsgi (pid=17417): Exception occurred processing WSGI script ‘/home/exampledotcom/flaskr/flaskr.wsgi’. [wsgi:error] Traceback (most recent call last): [wsgi:error] [pid 17417] File “/home/exampledotcom/flaskr/flaskr.wsgi”, line 4, in [wsgi:error] [pid 17417] from flaskr import app as application [wsgi:error] [pid 17417] File “/home/exampledotcom/flaskr/flaskr.py”, line 14, in [wsgi:error] [pid 17417] from sqlite3 import dbapi2 as sqlite3 [wsgi:error] [pid 17417] File “/usr/local/lib/python3.6/sqlite3/__init__.py”, line 23, in [wsgi:error] [pid 17417] from sqlite3.dbapi2 import * [wsgi:error] [pid 17417] File “/usr/local/lib/python3.6/sqlite3/dbapi2.py”, line 27, in [wsgi:error] [pid 17417] from _sqlite3 import * [wsgi:error] [pid 17417] ModuleNotFoundError: No module named ‘_sqlite3’ [core:info] [pid 17417] AH00128: File does not exist: /home/exampledotcom/public_html/500.shtml

Note that, to make the above lines from my flaskr error log more readable, I’ve removed the timestamps, process IDs, and requesting IP address. Here’s what one of those lines would look like with those bits left in:

[Wed Apr 19 19:27:15.518014 2017] [wsgi:error] [pid 17417] [client 112.213.114.126:52163] ModuleNotFoundError: No module named '_sqlite3'

A moment’s Googling turned up a number of relevant-looking pages. The kernels of wisdom contained in the top two answers here essentially did the trick: install the libsqlite3-dev package on the server and recompile Python.

When I executed yum -y install libsqlite3-dev, however, Yum told me:

No package libsqlite3-dev available.
Error: Nothing to do

I only thrashed around a little bit (e.g. I did yum clean all and hoped it might miraculously result in a second try at yum -y install libsqlite3-dev somehow working) before figuring out the issue. Running yum list | grep -i sqlite | grep dev yielded the following:

sqlite-devel.x86_64                         3.7.17-8.el7               @base
golang-googlecode-sqlite-devel.i686         0-0.9.hg74691fb6f837.el7   base
golang-googlecode-sqlite-devel.x86_64       0-0.9.hg74691fb6f837.el7.centos
libodb-sqlite-devel.x86_64                  2.3.0-1.el7                epel
soci-sqlite3-devel.x86_64                   3.2.3-1.el7                epel
sqlite-devel.i686                           3.7.17-8.el7               base
sqlite-devel.x86_64                         3.7.17-8.el7               base
sqlite2-devel.x86_64                        2.8.17-17.el7              epel
vsqlite++-devel.x86_64                      0.3.13-3.el7               epel

Of these, sqlite-devel.x86_64 seemed like the best bet, so I installed it (yum -y install sqlite-devel.x86_64) and moved into the Python 3.6.1 source directory and recompiled Python 3.6.1 like so:

./configure --enable-shared --enable-loadable-sqlite-ex 
tensions && make && make install

After restarting Apache (apachectl restart), I was good to go.