Spatialite for Python does not work on OSX

The default Python shipped with OSX is not compatible with Spatialite. Lucky, there is a solution for that. Find more inside!

Posted August 08th, 2012 in python, sqlite, spatialite, osx

Is your OSX ready to work with the GIS extension for SQLite? Try doing this on a Python shell:

>>> import sqlite3
>>> conn = sqlite3.connect(':memory:')
>>> conn.enable_load_extension(True)
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'sqlite3.Connection' object has no attribute 'enable_load_extension'

If you get the same error, welcome aboard!

The problem is that the Python interpreter shipped with OSX is not build with loadable extension support.

I'll quote the official documentation (I have had some troubles finding it):

[1] (1, 2) The sqlite3 module is not built with loadable extension support by default, because some platforms (notably Mac OS X) have SQLite libraries which are compiled without this feature. To get loadable extension support, you must modify setup.py and remove the line that sets SQLITE_OMIT_LOAD_EXTENSION. Source: http://docs.python.org/library/sqlite3.html#f1

How can I solve it?

You must install Python from scratch, enabling the loadable extension support at compile time.

You don't want to mess with sources and compilers? Me neither, so check my How to setup correctly OSX for Python development