I am trying to spatially enable my sqlite database:

import sqlite3
db = r'c:\temp\mydata.sqlite'
conn = sqlite3.connect(db)
cur = conn.cursor()
cur.execute(r"SELECT load_extension("<path>\libspatialite-4.dll");")
del conn

Python just stops working when I run this code. It doesn't crash in windows 7 x64, it just closes. It's very strange. No error is raised.

When I run the commands directly from python.exe in the command propmpt, I see python crashing: spatialite crashing python

Am I doing anything wrong?

  • Looks like you are using a 32 bit version of python. Are you definitely using a 32 bit version of spatialite? – BradHards Feb 8 '14 at 10:58
  • Can you use try catch for more details ? – user3120 Feb 10 '14 at 5:00
  • @BradHards - Yes I'm using a 32-bit version of spatialite. – code base 5000 Feb 10 '14 at 11:36
  • What are the details of your binaries? Are you using Arc built python and/or spatialite? How new is the spatialite build? Are spatialites dependencies available(geos, iconv, etc)? – user10353 Feb 14 '14 at 17:19

The version of sqlite3.dll included with Python doesn't seem to want to play nice with Spatialite. The only thing I could get to work (short of compiling everything from source) was:

  1. Download SQLite (or cyqlite - a recompile of SQLite for Windows with some handy features enabled, such as R-Tree so you can do spaital indexes) i.e. sqlite-dll-win32-x86-[version].zip
  2. Download mod_spatialite (Windows binaries are in the pink box at the bottom of the page) i.e. mod_spatialite-[version]-win-x86.7z
  3. Unzip first SQLite/cyqlite then mod_spatialite into the same folder (overwrite if there are any conflicts)
  4. Add this folder to your system Path
  5. Rename the sqlite3.dll that is in your Python DLLs directory, to something like sqlite3_old.dll, so that Python will use the new one on your path

See this blog post for more info.


this thread may be a bit old - but i ran into a somewhat similar issue a couple of months back

i think this post was also related - Sqlite, Python 2.7 and Spatialite

i ended up finding another build of libspatialite-4.dll here http://latuviitta.org/documents/Spatialite_4.0_test_with_jre_1.6.zip although it is also labeled as 'test'...

after placing this build of libspatialite-4 in my python27/DLLs, it seemed to work.


Are you sure that you have the library name correct?

For Python 2.7 and Spatialite 4 that are shipped with ArcGIS Desktop 10.2 I have to do the following:

import sqlite3

db = sqlite3.connect(dbpath)

I believe that db.load_extension and executing the function in SQL should have the same effect, but I do always use load_extension.

  • When try using this code, I get sqlite3.OperationalError: The specified module could not be found. I do have ArcGIS 10.2.1 installed btw. – code base 5000 Feb 7 '14 at 11:05
  • I also tried using the full path to the ArcGIS Bin folder as well. – code base 5000 Feb 7 '14 at 11:09
  • @josh1234 Well you do have a slightly newer version than I do (My python is 2.7.3). The library should be located in the ArcGIS bin directory (C:\Program Files (x86)\ArcGIS\Desktop10.2\bin. It may be named something other than spatialite400x.dll in 10.2.1. – Evil Genius Feb 7 '14 at 11:54
  • hmm, I looked in the file location, and the file is the same. – code base 5000 Feb 10 '14 at 11:36

Try this, please:

cur.execute(r'SELECT load_extension("<path>\libspatialite-4.dll");')

instead of your cur.execute line

And see this, also. They solved by putting all the dlls in the System32 folder even on a Windows 7 - 64 bits.

  • I tried that, it didn't work as well – code base 5000 Feb 7 '14 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.