Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to override an old sqlite3 module with pysqlite3 in django settings.py
# first: pip install pysqlite3-binary
# then in settings.py:
# these three lines swap the stdlib sqlite3 lib with the pysqlite3 package
__import__('pysqlite3')
import sys
sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
@defulmere

This comment has been minimized.

Copy link
Owner Author

@defulmere defulmere commented Oct 3, 2019

Django won't work with SQLite < 3.8.3 which is a problem on shared web hosts with Linux systems that have Python built against an older version of SQLite (looking at you, CentOS 7).

Some alternatives are:

  • Build a newer SQLite from source and then build Python against that, or...
  • Build a newer SQLite from source and set LD_LIBRARY_PATH to point at the updated libraries

Neither of these are ideal for most shared hosting customers who don't want to maintain their own custom builds, so IMO overriding sqlite3 (from the standard library) with pysqlite3 is a decent workaround.

@adhamselman

This comment has been minimized.

Copy link

@adhamselman adhamselman commented Feb 24, 2020

This was helpful. Thank you!

@becker929

This comment has been minimized.

Copy link

@becker929 becker929 commented Jul 25, 2020

Looks like you solved my problem! Thank you so much!

And for those googling:

I am trying to install Wagtail CMS on the host Webfaction while following the tutorial here: https://www.youtube.com/watch?v=1_yA25ZmNkA&list=PLMQHMcNi6ocsS8Bfnuy_IDgJ4bHRRrvub.

I was able to use Python's built-in venv to get around the fact that I couldn't use virtualenv or pipenv. However, I got stuck at
$ python3 manage.py migrate

I had the same error as above, where Django was complaining about needing SQLite version 3.8.3 or greater.

Then I followed the advice that support gave me:
"The latest versions of django 3 do not work with the sqlite3 version installed on our servers.
On our django installers we include a statically linked version of a newer sqlite version, as per https://github.com/coleifer/pysqlite3#building-a-statically-linked-library
You can run something similar within your virtualenv or try using the binary version."

... And I created a new environment, pip3 installed pysqlite3-binary, pip3 installed --upgrade django... but still got the same error when trying to migrate.

However, I went to mysite/settings/base.py and popped in the exact 3 lines you posted above, and voilà-- I can manage.py migrate!!

@aleonsan

This comment has been minimized.

Copy link

@aleonsan aleonsan commented Mar 25, 2021

Thanks!

Worked for me, using pipenv in Debian 9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment