4

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?

1
  • you need a C compiler and the sources of SQLite for building and installing the dependency libraries
    – gene
    May 9, 2014 at 18:22

2 Answers 2

1

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

4
  • I already tried the solution proposed in the link: gis.stackexchange.com/questions/75906/… . I use Python 2.7 now, and have downloaded the lib-spatialite-2.dll extension. But on the line when I'm connecting to it (conn.execute('SELECT load_extension("C:\Python27\libspatialite-2.dll")')), I get an error, that it can't find that resource, even though I'm certain it is in the directory I specified. May 10, 2014 at 15:30
  • And by the way, I use a 32-bit version. Maybe it's better to use 64? May 10, 2014 at 16:48
  • Try load_extension("C:\\Python27\\libspatialite.dll") (i.e. with double backslashes).
    – BradHards
    May 10, 2014 at 22:53
  • I have another problem now, I've no idea why, but suddenly it says: AttributeError: 'pysqlite2.dbapi2.Connection' object has no attribute 'enable_load_extension'. I've read, that I should change the setup.cfg file to enable this method, but I'm not using Linux. May 12, 2014 at 7:06
0

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, so that Python will use the new one on your path

See this blog post for more info.

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.