[GIS] Configuring spatialite database access for Python 2.6.6

databasepythonspatialitesqlite

I'm working on a GIS project, and I would like to implement and test some geo-spatial algorithms in Python. For this purpose, I will not only need sqlite, but also spatialite, in order to store and query the location data. Now I've tried to install the pyspatialite package, but no matter what Python version I tried (I tried all versions from 2.6 to 3.3), the pip keeps insisting, that none of the existing pyspatialite packages are compatible with my version of Python.

If I try to do this using easy_install, I get a traceback and an error:

AttributeError: MSVCCompiler instance has no attribute 'compiler'

And that also occurs, if I try to install the package manually, by executing the setup.py file.

From what I've already searched, some people suggest to connect to a spatialite database somehow using sqlite and loading extension, but frankly I have no idea how to do it, and couldn't understand any of these answers. I will be really grateful if someone here is able to propose a solution in a clear, step-by step way, as I'm not a very experienced Python programmer yet. Thanks in advance.

UPDATE:

Another attempt, this time with Python 3.3.5. The following code:

import sqlite3
conn = sqlite3.connect(":memory:")
conn.enable_load_extension(True)
conn.execute('SELECT load_extension("libspatialite-2.dll")')

yields:

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    conn.execute('SELECT load_extension("libspatialite-2.dll")')
sqlite3.OperationalError: %1 is not a valid Win32 application.

And again, I don't seem to resolve this on my own. I'm really stuck here, and slowly running out of ideas. If you can make it work on any Python version, I'd be REALLY relieved.

#

UPDATE #2

Sorry, the error above was due to something else, I reinstalled Python and pysqlite, and we are back with the old error. There are two options now:

1) I compile using from pysqlite2 import dbapi2 as sqlite3. In this case code is the following:

from pysqlite2 import dbapi2 as sqlite3
conn = sqlite3.connect(":memory:")
conn.enable_load_extension(True)
conn.execute('SELECT load_extension("DLLs\\libspatialite-4.dll")')
curs = conn.cursor()

In that case, the error is:

Traceback (most recent call last):
  File "C:\Users\mszydlowski\Desktop\Project\sqlite.py", line 3, in <module>
    conn.enable_load_extension(True)
AttributeError: 'pysqlite2.dbapi2.Connection' object has no attribute  'enable_load_extension' 

2) I compile using import sqlite3. In this case code is the following:

import sqlite3
conn = sqlite3.connect(":memory:")
conn.enable_load_extension(True)
conn.execute('SELECT load_extension("DLLs\\libspatialite-4.dll")')
curs = conn.cursor()

And the error:

Traceback (most recent call last):
  File "C:\Users\mszydlowski\Desktop\Project\sqlite.py", line 4, in <module>
    conn.execute('SELECT load_extension("DLLs\\libspatialite-4.dll")')
OperationalError: The specified module could not be found.

even though the file is certainly in there, and I did use the double backslashes, like I was already advised.

Any ideas?

Best Answer

As I said, , you need a Unix like C compiler and the sources of SQLite for build and install the dependency libraries (AttributeError: MSVCCompiler instance has no attribute 'compiler') and Windows don't have native compilers as Linux or Mac OS X, but you can try, look at Build pyspatialite on Windows, or Installation of Pyspatialite on Windows, for example.

You don't need Pyspatialite to connect to Spatialite via Python. You can use the latest version of Pysqlite, look at special python library needed for spatialite? or even the sqlite3 standard module of Python. You can download a Pysqlite version for Windows at Christoph Gohlke's Unofficial Windows Binaries for Python Extension Packages