Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite3.Connection' object has no attribute 'enable_load_extension

Tags:

python

sqlite

On python3.7.4 loading sqlite3 modules and requesting to enable_load_extension gives:

import sqlite3
conn=sqlite3.connect("./tests/data/ne_110m_admin_0_countries.sqlite")
conn.enable_load_extension(True)

AttributeError: 'sqlite3.Connection' object has no attribute 'enable_load_extension'

I understand the default Ubuntu sqlite3 package is building with load_extension deactivated. I have followed this guideline: https://charlesleifer.com/blog/compiling-sqlite-for-use-with-python-applications/

Basically, compiled sqlite3 with flag: -DSQLITE_ENABLE_LOAD_EXTENSION, using pyenv and building python 3.7.4 on verbose mode I can see the load extension flag being used, also following hte above tutorial and reinstalling pysqlite3 on pyenv

Running python on verbose mode:

>>> import sqlite3
# /home/jesus/.pyenv/versions/3.7.4/lib/python3.7/sqlite3/__pycache__/__init__.cpython-37.pyc matches /home/jesus/.pyenv/versions/3.7.4/lib/python3.7/sqlite3/__init__.py

The path to the module is correct.

Using the sqlite3 client:

jesus@earth:~/.pyenv/versions/3.7.4/bin$ sqlite3
SQLite version 3.31.0 2019-11-16 12:04:38
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> SELECT sqlite_compileoption_used('ENABLE_LOAD_EXTENSION');
1

I see that sqlite was build with the proper options

Never the the less I continue to have the same error of: AttributeError: 'sqlite3.Connection' object has no attribute 'enable_load_extension'

Update Requesting by SQL, if the library was compiled with load extension loading the reply is positive

cursor=conn.cursor() 
res=cursor.execute("SELECT sqlite_compileoption_used('ENABLE_LOAD_EXTENSION');") 
res.fetchall() [(1,)]

I am lost on what more can I do to debug the problem. This is happening on pyenv build

Any tips??

like image 788
Jorge Mendes Avatar asked Nov 16 '19 15:11

Jorge Mendes


1 Answers

The python has to be installed with augmented system variables for SQLite and python options. This works for me:

# Do the following in your shell
LDFLAGS="-L/usr/local/opt/sqlite/lib -L/usr/local/opt/zlib/lib" CPPFLAGS="-I/usr/local/opt/sqlite/include -I/usr/local/opt/zlib/include" PYTHON_CONFIGURE_OPTS="--enable-loadable-sqlite-extensions" pyenv install 3.7.6
like image 105
francbartoli Avatar answered Nov 08 '22 18:11

francbartoli