django.core.exceptions.ImproperlyConfigured:SpatiaLite需要将SQLite配置为允许扩展加载 [英] django.core.exceptions.ImproperlyConfigured: SpatiaLite requires SQLite to be configured to allow extension loading

查看:141
本文介绍了django.core.exceptions.ImproperlyConfigured:SpatiaLite需要将SQLite配置为允许扩展加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Django项目安装了spatialite,但是当我尝试迁移它时,向我显示此错误:

I installed spatialite for Django project but when I try to migrate it show me this error :

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/django/contrib/gis/db/backends/spatialite/base.py", line 44, in get_new_connection
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: SpatiaLite requires SQLite to be configured to allow extension loading.

我不知道该怎么办. 我通过更改libexec/setuptools/来尝试 setup.cfg文件

I don't know how to figure out. I tried this by changing libexec/setuptools/setup.cfg file

[build_ext]
 #define=
include_dirs=/Library/Frameworks/SQLite3.framework/unix/include
library_dirs=/Library/Frameworks/SQLite3.framework/unix/lib
libraries=sqlite3
#define=SQLITE_OMIT_LOAD_EXTENSION

我通过brew安装了spacealite,然后按如下所示更改了settings.py:

I have installed spatialite by brew then I changed the settings.py like this:

DATABASES = {
 'default': {
    'ENGINE': 'django.contrib.gis.db.backends.spatialite',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
 }
}
SPATIALITE_LIBRARY_PATH='/usr/local/lib/mod_spatialite.dylib'

推荐答案

问题

当使用基于默认sqlite3库的Python构建时,在django.contrib.gis.db.backends.spatialite.base中会出现ImproperlyConfigured错误-MacOSX和大多数Linux发行版就是这种情况,它是通过扩展加载

The Issue

The ImproperlyConfigured error is raised in django.contrib.gis.db.backends.spatialite.base when using a Python build based on the default sqlite3 library - which is the case for MacOSX and probably most Linux distros - which was built with extension loading disabled. So, this won't work:

conn.enable_load_extension(True)
conn.load_extension(SPATIALITE_LIBRARY_PATH)

按照 GeoDjango的说明进行操作在大多数情况下无法解决问题.甚至可以通过 Homebrew spatialite-tools >仅在Homebrew目录中安装新的spacespaceite和sqlite可执行文件和库.

Following the directions of the GeoDjango won't solve the problem in most cases. Even installing the spatialite-tools via Homebrew only installs new spatialite and sqlite executables and libraries in the Homebrew directory.

假设已安装sqlite和spacespaceite(例如通过Homebrew),并且相应的sqlite版本已启用load_extention,则可以通过链接此sqlite库从头开始构建Python.使用pyenv很容易.可以通过PYTHON_CONFIGURE_OPTS环境变量提供特定的构建选项(详细信息此处)并设置CPPFLAGSLDFLAGS(请参见此处)

Assuming sqlite and spatialite are installed (e.g. via Homebrew) and the respective sqlite version has load_extention enabled, you can build Python from scratch with this sqlite library linked. It's pretty easy using pyenv. Specific build options can be provided via the PYTHON_CONFIGURE_OPTS environment variable (details here) and setting CPPFLAGS and LDFLAGS (see here)

使用pyenv进行构建,假设通过自制软件安装了sqlite(有关安装哪个版本以及安装在何处的详细信息,请检查which sqlite3brew info sqlite):

Building with pyenv assuming sqlite was installed via homebrew (check which sqlite3and brew info sqlite for details on which versions are installed and where):

PYTHON_CONFIGURE_OPTS="--enable-loadable-sqlite-extensions --enable-optimizations --with-openssl=\$(brew --prefix openssl)" \
LDFLAGS="-L/usr/local/opt/sqlite/lib" \
CPPFLAGS="-I/usr/local/opt/sqlite/include" \
pyenv install 3.8.2

如果缺少依赖项(例如openssl),请参见下文中的手动构建Python.

If dependencies are missing (e.g. openssl), see below on building Python manually.

最后,在您的Django设置中引用动态链接的spacespaceite非常重要(因此请确保/usr/local/lib/mod_spatialite.dylib存在)

Finally, it's important to reference the dynamically linked spatialite in your Django settings (so make sure /usr/local/lib/mod_spatialite.dylib exists)

SPATIALITE_LIBRARY_PATH = '/usr/local/lib/mod_spatialite.dylib'
DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.spatialite',
        'NAME': os.path.join(BASE_DIR, 'db.spatialite3'),
    }
}

手动构建SQLite和Python

要在启用了负载扩展的情况下从源代码编译sqlite(请参阅文档),请下载合并版本网站上的源文件,并按照说明进行操作.您将要包含构建选项-DSQLITE_ENABLE_RTREE,但 -DSQLITE_OMIT_LOAD_EXTENSION

Build SQLite and Python manually

To compile sqlite from source with load extensions enabled (see docs), download the amalgamated source file from the website and follow the instructions. You'll want to include the build option -DSQLITE_ENABLE_RTREE but NOT -DSQLITE_OMIT_LOAD_EXTENSION!

编译sqlite3后,运行可执行文件并通过命令.dbconfig

Once sqlite3 compiled, run the executable and check via the command .dbconfig

sqlite > .dbconfig
[...]
load_extension on
[...]

默认情况下会关闭加载扩展程序,以避免安全问题.

Loading extensions is turned off by default, to avoid security issues.

手动构建Python也非常简单,并且遵循与通过pyenv安装类似的模式.满足依赖关系后(例如,在 macos brew install openssl xz gdbm上),请使用所需的版本,并设置编译选项以启用sqlite扩展加载(并告诉make在哪里找到新编译的SQLite构建):

Building Python manually is also pretty straightforward and follows a similar pattern as installing via pyenv. Once dependencies are met (e.g. on macos brew install openssl xz gdbm), you download the tarball with the desired version and set the compile options to enable sqlite extension loading (and telling make where to find your newly compiled SQLite build):

./configure --enable-loadable-sqlite-extensions --enable-optimizations --with-openssl=$(brew --prefix openssl)
LDFLAGS="-L<path-to-sqlite>" \
CPPFLAGS="-I<path-to-sqlite>" \
make -j2

其他解决方案(和其他系统)

所描述的解决方案主要适用于MacOS和Python3,因为sqlite3是Python3的标准python库的一部分,而不是Python 2的单独软件包.以前的解决方案(例如[pysqlite])仅适用于Python2, EOL .

您尝试的内容可能基于较旧的解决方案,可能已应用于Python2.

What you tried may have been based on an older solution, which may have applied to Python2.

此外,似乎还提出了其他Windows解决方案使用cyqlite

Also, there seemed to have been proposed other solutions for Windows using cyqlite

这篇关于django.core.exceptions.ImproperlyConfigured:SpatiaLite需要将SQLite配置为允许扩展加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆