3

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 which is 2.6.6.

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.

Are you able to propose a solution in a clear, step-by step way?

I'm not a very experienced Python programmer yet.


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 able to resolve this on my own.


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' 
  1. 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.

0

2 Answers 2

2

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

0
1

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 and acknowledge you have read our privacy policy.

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