sqlite ImportError: No module named _sqlite3 Google App Engine


I am attempting to deploy a Python Flask, HTML, JavaScript web application through Google App Engine.

I’m using sqlite3 for my database

 >>> python
 >>> import sqlite3

This works just fine. Deploying my web app with

$ gcloud app deploy

Works just fine.

However when I visit the page, I receive this message

Error: Server Error

The server encountered an error and could not complete your request.
Please try again in 30 seconds.

And when I check the Error Reporting at https://console.cloud.google.com/home/dashboard
I get this error

Traceback (most recent call last):
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 311, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/base/data/home/apps/p~live-business-solutions/20191010t190446.421644656225294026/webapp.py", line 9, in <module>
    from databasefunctions import *
  File "/base/data/home/apps/p~live-business-solutions/20191010t190446.421644656225294026/databasefunctions.py", line 5, in <module>
    import sqlite3
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_dist/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
    from dbapi2 import *
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/941d77da994078b1/python27/python27_dist/lib/python2.7/sqlite3/dbapi2.py", line 28, in <module>
    from _sqlite3 import *
ImportError: No module named _sqlite3

It is from the line

import sqlite3

of my databasefunctions.py file. I’m not sure why this is, could anyone please help?

I was wondering if I am supposed to somehow include sqlite3 in requirements.txt? and if so how I would do this?

Asked By: shortone456



From this answer:

The sqlite3 is an optional part of the standard library.

So it is not installed via pip and thus using the requirements.txt file. But it needs some package level dependencies libsqlite3-dev or sqlite-devel that don’t come in the Google App Engine Python3 runtime. So it is not possible to use sqlite3 on Google App Engine Standard.

The solution is to use Cloud Run, which will allow you to run your application in a Docker container and you will be able to customize your runtime. You can start with this Quickstart guide or check this answer that I provide about the same issue with other library.

Answered By: llompalles

Try the below commands in centos for get ride of GCP slite3 import error during docker commands execution:

yum install -y gcc make sqlite-devel zlib-devel libffi-devel openssl-devel

Need reconfigure python 3+version

cd python3.9.6
make clean
make && make altinstall
sudo ln -s /usr/local/bin/python3 /usr/bin/python3

while checking /bin/python3 it has to return the 3.9.6 , if you are going to use 3.6 then that has to be reconfigured and symlink has to be created.

sudo ln -sf /usr/bin/python2.7 /usr/bin/python (Must to run otherwise YUM commands are not running)

/bin/python3 –version

Answered By: kalaivani