How to change SQLite version used by Python? - python

I have Python 3.8 installed alongside SQLite 3.16.2 on Debian 9.12 and I need to upgrade to a newer version of SQLite. I've downloaded and compiled the amalgamation available on the SQLite's site, and put it in /usr/bin, so when I do
$ sqlite3 --version
I get 3.32.3 in response (which is the version I compiled).
However, when I do
$ python3.8
>>> import sqlite3
>>> sqlite3.sqlite_version
I get 3.16.2, which is the earlier version. How do I change the SQLite version picked up by Python?
mkrieger1 suggested that this question may answer mine. It won't work here, as the solution provided there is directed at Python 2, not Python 3. pysqlite2 does not work with Python 3.

In my case I cannot replace with newer version because I cannot find these files. (I installed the sqlite-autoconf-3350500)
I used another manner to let it work just execute below command
export LD_LIBRARY_PATH="/usr/local/lib"
>>> import sqlite3
>>> sqlite3.sqlite_version
'3.35.5'

The way I would go around it, is by finding out the path to the old sqlite version that you are importing:
import sqlite3
print(sqlite3.__file__)
For me, this outputs:
C:\Users\YOUR_USERNAME_HERE\AppData\Local\Programs\Python\Python38\lib\sqlite3\__init__.py
Go to the lib path:
C:\Users\YOUR_USERNAME_HERE\AppData\Local\Programs\Python\Python38\lib
Then find the sqlite3 folder and delete it, then replace it with your up-to-date version. Re-try:
>>> import sqlite3
>>> sqlite3.sqlite_version
You should get your new version.

Related

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.
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:
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).
You'll need to install the library, and...
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.
If you want 2 different version of sqlite3 (python3) on 2 different environments, you can do that.
Since you mentioned that sqlite3 is part of the std library, it seems like you can try the pysqlite3 package instead.
If you can't run pip, run the following command first.
sudo apt install python3-pip
Then,
pip install virtualenv
python3 -m venv sqlitev1 #(whatever name you want)
source sqlitev1/bin/activate
pip install pysqlite3==0.4.4 #(this can be whatever version u want)
source deactivate
python3 -m venv sqlitev2 #(whatever name you want)
source sqlitev2/bin/activate
pip install pysqlite3==0.4.4 #(this can be whatever version u want)
source deactivate
Now you have 2 python environments, sqlitev1 and sqlitev2, with 2 different version of sqlite3.
It might be super hacky but you can make the new version of sqlite and then make sure that the path pointing to the new version is on the pythonpath environment before the built in one. Python will scan the python path from first to last to find an import, so the new version first in the python path for the processes that want the new version and then exclude that path with the old processes that need the built in one. You can accomplish this with a bash script that loads the env and then runs the python process for the new services.
Again this is super hacky so last resort.
If you want a different version of Sqlite than that installed with your distro, and a Python that uses that version, then you could
Compile sqlite to an alternative location
Compile Python to a different location, and point it to the custom Sqlite installation.
The "pointing" is covered in the accepted answer to this question. The question body itself shows how you might compile sqlite to a custom location.
The other answer to that question proposes setting the LD_LIBRARY_PATH environment variable to the directory containing the custom sqlite build to avoid having to compile Python. This might work with a virtualenv (it could be set in the preactive hook, for example).
See also
What is LD_LIBRARY_PATH and how to use it?
Set LD_LIBRARY_PATH before importing in python
Another approach would be to compile pysqlite3 in a virtualenv over the custom sqlite build. You can read about this in this blog post (I won't copy the details as it isn't clear what licence is used by the blog).

Problem upgrading Sqlite3 version on CentOS for Python

I have CentOS 6 on my system and I'm trying to update SQLite for Python. I've installed it from source and executing sqlite --version returns version 3.33.0 as expected.
However, when I try to check the python SQLite version using import sqlite3; sqlite3.sqlite_version; I still get the previous SQLite version 3.6.20.
Software Locations:
Python 3.6.9 - /usr/bin/python3
Sqlite3 - /usr/bin/sqlite3
I've tried the solution here, this does not work at all, after updating LD_LIBRARY_PATH and checking the python SQLite version it still gives '3.6.20', and here, when I try sudo LD_RUN_PATH=, it gives me the error No such file or directory, but when I execute it without sudo LD_RUN_PATH=, it successfully compiles but still gives me SQLite '3.6.20' (Compiled python without uninstalling).
Note: I have multiple python3 versions.
What can I do to resolve this?
When I did it (specifically trying to find a way to update sqlite3 for a running python program; did not work...), I compiled sqlite and got libsqlite3.so.0.8.6, and then replaced the system-wide sqlite3 with that. For me on debian, that was in /usr/lib/x86_64-linux-gnu. I did see (though now I can't find where) that this way may cause issues when updating in the future. It did update python's sqlite3 for me though.
You can import specific versions:
__requires__= 'sqlite3==3.6.20'
import pkg_resources
pkg_resources.require("sqlite3==3.6.20")
import sqlite
Note that this only works on the first import. If sqlite gets imported before pkg_resources, it will take the latest version.

Where can I find pre-installed packages that comes with Python IDLE?

I'm more specifically wanting to know whether sqlite3 and json comes with python IDLE or do I have to install them separately to use them inside IDLE, If so can anyone link me to those installing procedures of sqlite3 and json on Python IDLE?
I also want to know where I can find the list of other pre-installed packages that comes with basic Python IDLE (i.e. Python 2.7.14) . I am a Beginner and it would be really helpful.
Thank you.
To get a list of your packages, from your terminal, launch python :
python
Then
help("modules")
Another solution, if you want to check if json or sqlite3 are installed, start Python from your terminal :
python
Then import sqlite3 and json:
import json
import sqlite3
You can check theirs version with :
>>> json.__version__
'2.0.9'
>>> sqlite3.version
'2.6.0'
IDLE is part of the CPython standard library and is usually installed when tkinter (and turtle) are. It provides an optional alternate interface for running your code with a particular python binary. When you run code through IDLE, it is run by the python binary that is running IDLE. So the same modules are available whether you run code through the terminal interface or through IDLE. There is no separate installation.

How do I 'down grade' to python2.7

Being quite cavalier I went for the latest version of Python (3.2.2)
Unfortunately it lacks the matplotlib that I desperately need.
I have downloaded python 2.7.
My simple question is weather I have to uninstall python3.2 or can I leave it on my windows 64 system?
you can keep the python 3.2 installation, but you will have to be carefull about which version you launch (by making the call explicit). you can always check the python version in a script by doing:
import sys
print sys.version
If you have no particular use of python 3.2, I recommend uninstalling it.
I'm not sure if there's a difference on python in windows but on mac and linux I simply call older versions using
python2.6 or python2.7
It seems to keep all the versions I previously had before. Maybe try running the above command and seeing if it works on windows and if it does just change the symbolic link 'python' is pointing to.

Sqlite version for Python 3.x

I want to use sqlite3 with Python 3.1.3 and I need to set enable_load_extension to true. To do this I believe I need sqlite version 3.x. From reading posts here it looks like a suitable version of sqlite ought to be bundled with python version 2.6 and up. However, when I do:
import sqlite3
sqlite3.version_info
The result returned is: '2.4.1'
I get the same answer on a different machine running Python 2.6.
The pysqlite site has no binaries for Python 3.x. My copy of Python came from the official Python site.
So:
1) What version of sqlite should I have with 3.1?
2) If I ought to have a more up to date version where has it gone - do I need to set an environment variable?
2) If I need to u
Don't confuse the version of SQLite with the version of pysqlite, the Python binding for the SQLite API. The version and version_info attributes you used refer to the latter.
Ever wondered why the module is named sqlite3? It only supports version 3.x!
To check the SQLite version, use sqlite_version instead:
import sqlite3
print sqlite3.sqlite_version
On my Python 2.6 installation, this prints 3.5.9. For Python 3.2, I get 3.7.4.
You can also use SQL to get the version:
>>> import sqlite3
>>> connection = sqlite3.connect(':memory:')
>>> cursor = connection.cursor()
>>> cursor.execute('SELECT sqlite_version()').fetchone()
('3.7.4',)
You need sqlite3.sqlite_version_info ... this is 3.5.9 for Python 2.6 and 3.1, 3.6.21 for Python 2.7, and 3.7.4 for Python 3.2. What you have got is the version of pysqlite.
Have you tried to "set enable_load_extension to true"?
You may wish to read some of this long saga ...

Categories

Resources