Python 中的 spaceite Sqlite load_extension 失败
我正在尝试使用 Spatialite beta 3.0 版,因为我正在使用 64 位计算机上的 Windows 7。
我总是遇到可怕的 sqlite3.OperationalError: The 找不到指定的模块。
当我尝试加载时出错 libspatialite-4.dll
。
我已尝试以下操作:
- 将
libspatialite-4.dll
和同一文件夹中的所有其他 dll - 使用 dll 的完整路径
- 将 dll 位置添加到
'PATH'
环境变量 - 将 dll 位置附加到 sys.path 属性作为 Python 代码的一部分
- 复制
c:\windows\system32
文件夹中的所有 dll(需重新启动计算机) - 复制所有动态链接库在
c:\windows\sysWoW64
文件夹中(重新启动机器后,这应该适用于 32 位 dll,但我还是尝试了)
我的代码如下:
import sqlite3
conn = sqlite3.connect(":memory:")
conn.enable_load_extension(True)
conn.execute('SELECT load_extension("libspatialite-4.dll")')
注意 - 我已经尝试过完整的路径也没有运气。我记得有 Windows XP 32 位也有同样的问题。我可以使用但不能 记住我做了什么:(
更新
我已经在 32 位 Windows 7 上测试了设置,并将所有 dll 放入 System32 文件夹中。因此,这表明 64 位存在一些问题我是否需要另一个版本的 MSVC(我不认为 Spatialite 网站说这是必要的,所以我可能只能猜测 - 我安装了 MSVC2010)?
I am trying to use the Spatialite beta version 3.0 because I am using
Windows 7 on a 64-bit machine.
I consistently get the dreaded sqlite3.OperationalError: The
error when I try to load
specified module could not be found.libspatialite-4.dll
.
I have tried the following:
- put
libspatialite-4.dll
and all the other dlls in the same folder - use the full path to the dlls
- add the dll location to the
'PATH'
environment variable - append the dll location to the sys.path attribute as part of the Python code
- copy all the dlls in the
c:\windows\system32
folder (complete with restart of the machine) - copy all the dlls in the
c:\windows\sysWoW64
folder (complete with restart of the machine this is supposed to be for 32 bit dlls but I tried it anyway)
my code is as follows:
import sqlite3
conn = sqlite3.connect(":memory:")
conn.enable_load_extension(True)
conn.execute('SELECT load_extension("libspatialite-4.dll")')
NOTE - I have tried the full path too with no luck. I remember having
the same problem with Windows XP 32-bit. I got it working but can't
remember what I did :(
UPDATE
I have tested the setup on 32-bit Windows 7 and putting all the dlls in the System32 folder works. So, this suggests that there is some problem with the 64-bit setup. Could it be that I need another version of MSVC (I don't think the Spatialite website says which is necessary so I might just have to guess - I have MSVC2010 installed)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Python 中包含的
sqlite3.dll
版本似乎并不想与 Spatialite 很好地配合。我唯一能开始工作的事情(除了从源代码编译所有内容之外)是:sqlite-dll-win32-x86-[版本].zip
mod_spatialite-[version]-win-x86.7z
sqlite3.dll
重命名为sqlite3_old.dll
之类的名称,以便 Python 将在您的路径上使用新的文件请参阅 这篇博文 了解更多信息。
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:sqlite-dll-win32-x86-[version].zip
mod_spatialite-[version]-win-x86.7z
sqlite3.dll
that is in your Python DLLs directory, to something likesqlite3_old.dll
, so that Python will use the new one on your pathSee this blog post for more info.
我遇到了同样的问题,这让我困惑了好几天。我在 Windows 7(x64) 上运行 Python 2.6(32 位),因此它可能与您的设置不同。
您可以尝试以下方法(摘自此 Google 网上论坛帖子 ):
问题似乎是由于用户权限设置,Windows 可能无法从 C:\Windows\system32 加载 dll。
I was having the same problem and it confused me for days. I'm running Python 2.6 (32-bit) on Windows 7(x64), so it may not be the same setup as you have.
Here's what you can try (taken from this Google Groups post):
The problems seems to be that Windows may not load the dlls from C:\Windows\system32 because of user permissions settings.
我最近刚刚经历了 pyspatialite 3.0.1 及其所有依赖库的 x64 构建的噩梦。这是可以做到的,但需要一些“调整”才能做到正确。
首先,请注意此处编译 spaceite.c 合并可能需要的解决方法:
Pyspatialite 3.0.1 Issue #7 Comment #3
其次,我建议您使用 MSVC 2008 / SDK 7.0 x64 进行编译,这就是 Python 2.7 x64 是用 .当我使用 mingw-w64 编译依赖库时,我在尝试正确编译时遇到了很多麻烦。
iconv (v. 1.9.2) 和 proj4 (v. 4.8.0) 似乎都可以很好地编译和安装;但是,使用最新的 GEOS svn_trunk (v. 3.3.5) 可能会遇到一些麻烦。请注意以下解决方法/修复的链接...
OSGEO GEOS TRAC Ticket #574
OSGEO GEOS TRAC Ticket #577
如果您从以下位置下载两个 makefile 577,他们在 574 中包含修复程序。
您还需要下载夜间快照之一,并在编译之前将 geos/src/triangulate 目录复制到您的构建文件夹,因为它在 svn_trunk 中丢失。
最后,您需要对 geos/src/dirlist.mk 进行细微修改:
在第 45 行,在“simplify \”下方和“util”上方添加“triangulate \”(无引号)。
现在,当您编译时,您可能会看到一些警告,但构建不应彻底失败......
这会照顾您的库依赖项。现在您需要执行以下两件事之一:
1.在 pyspatialite 构建文件夹中创建 setup.cfg 文件并添加 /bin、/lib 和 /include 路径,或者
2.直接编辑pyspatialite setup.py文件,同样操作。
我发现直接编辑 setup.py 文件最简单,并将路径添加到依赖库,看起来类似于:
如果进行这些更改后 pyspatialite 仍然无法为您构建,则对 setup.py 进行另一组修改:在第 121 行周围,添加以下行...
请记住替换路径以匹配您的特定设置。应该可以做到这一点。运行“python setup.py install”后,一切都应该正常。
您可以运行 ../Python27/Lib/site-packages/pyspatialite/test 中的所有测试 - 它们都通过了我的测试;但是,更好、更实际的测试可能是运行此链接中的示例代码:
SpatiaLite 和 Python
作者介绍的步骤并未涵盖使依赖库在 x64 位环境中工作的细节,但是,我没有发现它们特别有用,因为pyspatialite 3.0.1 现在会自动检测要下载的 spaceite 合并的适当版本。该网站上的示例代码创建了一个 Spatialite 数据库文件,并在其中填充了数千个条目。一切对我来说都很成功;所以我相信上面概述的获得 pyspatialite x64 构建的方法是有效的。
祝你好运!
-RMW混沌
I just recently went through a nightmare of a x64 build of pyspatialite 3.0.1 and all its dependent libraries. It can be done, but takes some "tweaking" to get it right.
First, note the workaround that may be needed to compile spatialite.c amalgamation here:
Pyspatialite 3.0.1 Issue #7 Comment #3
Second, I suggest you compile with MSVC 2008 / SDK 7.0 x64, which is what Python 2.7 x64 is compiled with. I ran into a great deal of trouble trying to get things to compile right when I compiled the dependency libs with mingw-w64.
iconv (v. 1.9.2) and proj4 (v. 4.8.0) both seem to compile and install just fine; however, you may run into some trouble using the latest GEOS svn_trunk (v. 3.3.5). Note the following links for workarounds/fixes...
OSGEO GEOS TRAC Ticket #574
OSGEO GEOS TRAC Ticket #577
If you download the two makefiles from 577, they include the fix in 574.
You will also want to download one of the nightly snapshots and copy the geos/src/triangulate directory over to your build folder before compiling, as it's missing in the svn_trunk.
Finally, you'll want to make a minor modification to geos/src/dirlist.mk:
At line 45, add 'triangulate \' (no quotes) just below 'simplify \' and above 'util'.
Now when you compile, you may see some warnings, but the build should not fail outright...
That takes care of your lib dependencies. Now you need to do one of two things:
1.Either create a setup.cfg file in your pyspatialite build folder and add the /bin, /lib, and /include paths, or
2.Directly edit the pyspatialite setup.py file and do the same.
I found it easiest to edit the setup.py file directly, and add the paths to the dependency libs to look similar to:
If after making these changes pyspatialite still fails to build for you, then make one more set of modifications to setup.py: around line 121, add the following lines...
Remember to replace the paths to match your particular setup. That should do it. After you run 'python setup.py install' everything should work.
You can run all the tests in ../Python27/Lib/site-packages/pyspatialite/test - they all passed for me; however, a better, more realistic test might be to run the example code from this link:
SpatiaLite and Python
The steps the author goes into don't cover the detail to get the dependency libs working in an x64 bit environment, however, and I didn't find them particularly useful as pyspatialite 3.0.1 now automatically detects the appropriate version of the spatialite amalgamation to download. The sample code on the site creates a spatialite database file and populates it with thousands of entries. Everything ran successfully for me; so I believe the method outlined above to get a pyspatialite x64 build works.
Good luck!
-RMWChaos
我按照此处。根据上面的一些内容,我的系统是 Win7 64 位,带有 32 位 spaceite 库和 python 的 pysqlite 库。我链接到 DLL 目录中的 sqlite3.dll(这是 ArcGIS 安装的 Python,与其他安装略有不同)。
这解决了通过 pysqlite 加载 Spatialite 4 dll 的问题。
I solved this by following instructions in the last post here. My system was, as per some above - Win7 64bit with 32-bit spatialite libraries and the pysqlite library for python. I linked to the sqlite3.dll in my DLLs directory (this is the Python that ArcGIS installs, so slightly different to other installations).
This solved the problem with loadinging Spatialite 4 dll via pysqlite.