Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple versions of Sqlite3 for Python on the same server

On a Linux server, I have some Python scripts using the built-in sqlite3 module (+ some Sqlite extensions built from source, as detailed in Upgrade Python's sqlite3 on Debian).

For another Python script, I need a newer version of the Sqlite shared library than the one I already have on the system. Reason: I need Sqlite higher than 3.25.0 for Window Functions.

If I install it from source here and do make and make install, it will probably overwrite previous versions of this library on the server, and could potentially break other OS tools using it.

How do you handle the general problem of having multiple versions of the Sqlite shared library?

I don't think Python virtual environments can be used for this context, or would it be possible?

Note: pip3 install --upgrade sqlite3 does not exist: we cannot upgrade Python's built-in sqlite3 package like this. And by the way we probably should not, since it could break some OS tools using Python + sqlite3.

like image 356
Basj Avatar asked Mar 22 '26 04:03

Basj


1 Answers

This is very tricky and will need a little code change in your scripts.

What to do:

  • First, check the sqlite3 library version included with python just in case:

    python -c "import sqlite3; print(sqlite3.connect(':memory:').execute('SELECT sqlite_version();').fetchall())
    

    In my computer (python 3.8, windows) the output is [('3.35.5',)] which means python has the sqlite 3.35.5 library. I have no sqlite installed in my system: this is the library that comes with python3.8.

  • IF your python sqlite3 library is not the one you need :-( you have an alternative: you can use the pysqlite3 instead of the sqlite3 standard library. In this case:

    1. You'll need to build the pysqlite3 library by yourself using the Sqlite3 'amalgamation' that matches the version you want to use (more on later).
    2. You'll need to install the library, and...
    3. You will need to change your python script imports import pysqlite3 as sqlite3 # instead of sqlite3

Ok, what is the 'amalgamation` and how to build pysqlite3?

The amalgamation is the whole sqlite3 library in just one .c file (with the sqlite3.h file). You can get it from the sqlite3 download page: sqlite3.36 amalgamation.

Once you have the amalgamation, follow the instructions to build statically pysqlite3, and install the package.

Now you can use pysqlite3 in your code.

like image 127
Iñigo González Avatar answered Mar 23 '26 18:03

Iñigo González



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!