Install Spatialite for python (GeoDjango) on OS X Install Spatialite for python (GeoDjango) on OS X sqlite sqlite

Install Spatialite for python (GeoDjango) on OS X


I was able to get this working now, using the tip here:
https://github.com/ghaering/pysqlite/issues/60#issuecomment-50345210

I'm not sure if it was using the real paths that fixed it, or just the Homebrew kegs or underlying packages have been updated and now install cleanly. Still, it works now.

I reproduce below the steps I took:

brew updatebrew install sqlite  # 3.8.5brew install libspatialite  # 4.2.0brew install spatialite-tools  # 4.1.1git clone https://github.com/ghaering/pysqlite.gitcd pysqlite

(where brew reported I had existing versions I unlinked them and installed the latest as commented above)

then edited setup.cfg to comment out #define=SQLITE_OMIT_LOAD_EXTENSION and specify the paths:

include_dirs=/usr/local/opt/sqlite/includelibrary_dirs=/usr/local/opt/sqlite/lib

activated the virtualenv where I want it installed, then

python setup.py buildpython setup.py install

(build_static still fails with clang: error: no such file or directory: 'sqlite3.c')

(maybe I should have done pip install . as suggested in the github issue)

now the spatialite geodjango.db "SELECT InitSpatialMetaData();" succeeds, albeit with an ignorable error:

InitSpatiaMetaData() error:"table spatial_ref_sys already exists"

i.e. it's probably not even necessary to run that command


When I was istalling this i follow this instructions https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/spatialite/#pysqlite2

pysqlite2

If you’ve decided to use a newer version of pysqlite2 instead of the sqlite3 Python stdlib module, then you need to make sure it can load external extensions (i.e. the required enable_load_extension method is available so SpatiaLite can be loaded).

This might involve building it yourself. For this, download pysqlite2 2.6, and untar:

$ wget https://pypi.python.org/packages/source/p/pysqlite/pysqlite-2.6.3.tar.gz$ tar xzf pysqlite-2.6.3.tar.gz$ cd pysqlite-2.6.3

Next, use a text editor (e.g., emacs or vi) to edit the setup.cfg file to look like the following:

[build_ext]#define=include_dirs=/usr/local/includelibrary_dirs=/usr/local/liblibraries=sqlite3#define=SQLITE_OMIT_LOAD_EXTENSION


I had the same error: SQLite header and source version mismatch.

For me it was enough to update libsqlite3-dev.

After that invoking $ spatialite geo.db "SELECT InitSpatialMetaData();" creates proper database.