{"title":"GeoDjango 安装","version":"6.0","locale":"zh-hans","docname":"ref/contrib/gis/install/index","url":"/zh-hans/6.0/ref/contrib/gis/install/","canonical":"https://djangodocs.dev/zh-hans/6.0/ref/contrib/gis/install/","summary":"概况 Link to this heading # 一般来说，GeoDjango 安装需要： Python 和 Django 空间数据库 地理空间库 以下各节提供了每项要求的细节和安装说明。此外，还提供了以下平台的具体说明： macOS Windows 使用源码 因为 GeoDjango…","html":"<h1>GeoDjango 安装<a class=\"heading-anchor\" href=\"#geodjango-installation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"overview\">\n<h2>概况<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>一般来说，GeoDjango 安装需要：</p>\n<ol class=\"arabic simple\">\n<li><p><a class=\"reference internal\" href=\"#django\"><span class=\"std std-ref\">Python 和 Django</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#spatial-database\"><span class=\"std std-ref\">空间数据库</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.0/ref/contrib/gis/install/geolibs/#geolibs-list\"><span class=\"std std-ref\">地理空间库</span></a></p></li>\n</ol>\n<p>以下各节提供了每项要求的细节和安装说明。此外，还提供了以下平台的具体说明：</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"#macos\"><span class=\"std std-ref\">macOS</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#windows\"><span class=\"std std-ref\">Windows</span></a></p></li>\n</ul>\n<aside class=\"admonition-use-the-source admonition\">\n<p class=\"admonition-title\">使用源码</p>\n<p>因为 GeoDjango 利用了最新的开源地理空间软件技术，所以需要最新版本的库。如果你的平台没有二进制包，可能需要从源码安装。当从源码编译库时，请严格按照说明进行，尤其是当你是初学者时。</p>\n</aside>\n</section>\n<section id=\"requirements\">\n<h2>依赖项<a class=\"heading-anchor\" href=\"#requirements\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"python-and-django\">\n<span id=\"django\"></span><h3>Python 和 Django<a class=\"heading-anchor\" href=\"#python-and-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>因为 GeoDjango 是包含在 Django 中的，所以请参考 Django 的 <a class=\"reference internal\" href=\"/zh-hans/6.0/topics/install/#installing-official-release\"><span class=\"std std-ref\">安装说明</span></a> 来详细了解如何安装。</p>\n</section>\n<section id=\"spatial-database\">\n<span id=\"id1\"></span><h3>空间数据库<a class=\"heading-anchor\" href=\"#spatial-database\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>目前支持的空间数据库包括 PostgreSQL（带有 PostGIS）、MySQL、Oracle 和 SQLite（带有 SpatiaLite）。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>推荐使用 PostGIS，因为它是最成熟、功能最丰富的开源空间数据库。</p>\n</aside>\n<p>The geospatial libraries required for a GeoDjango installation depends\non the spatial database used. The following lists the library requirements,\nsupported versions, and any notes for each of the supported database backends:</p>\n<div class=\"table-scroll\" role=\"region\" tabindex=\"0\" aria-label=\"Table\"><table class=\"docutils align-default\">\n<thead>\n<tr class=\"row-odd\"><th class=\"head\"><p>数据库</p></th>\n<th class=\"head\"><p>库依赖项</p></th>\n<th class=\"head\"><p>支持的版本</p></th>\n<th class=\"head\"><p>注意</p></th>\n</tr>\n</thead>\n<tbody>\n<tr class=\"row-even\"><td><p>PostgreSQL</p></td>\n<td><p>GEOS, GDAL, PROJ, PostGIS</p></td>\n<td><p>14+</p></td>\n<td><p>需要 PostGIS。</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>MySQL</p></td>\n<td><p>GEOS, GDAL</p></td>\n<td><p>8.0.11+</p></td>\n<td><p><a class=\"reference internal\" href=\"/zh-hans/6.0/ref/contrib/gis/db-api/#mysql-spatial-limitations\"><span class=\"std std-ref\">功能受限</span></a>。</p></td>\n</tr>\n<tr class=\"row-even\"><td><p>Oracle</p></td>\n<td><p>GEOS, GDAL</p></td>\n<td><p>19+</p></td>\n<td><p>不支持 XE。</p></td>\n</tr>\n<tr class=\"row-odd\"><td><p>SQLite</p></td>\n<td><p>GEOS, GDAL, PROJ, SpatiaLite</p></td>\n<td><p>3.31.0+</p></td>\n<td><p>需要 SpatiaLite 4.3+</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n<p>关于 PostgreSQL／PostGIS／GEOS／GDAL的可能组合，也请参见 OSGeo Wiki 上的 <a href=\"#id23\"><span class=\"problematic\" id=\"id24\">`这个比较矩阵`__</span></a> 。</p>\n</section>\n</section>\n<section id=\"installation\">\n<h2>安装<a class=\"heading-anchor\" href=\"#installation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"geospatial-libraries\">\n<h3>地理空间库<a class=\"heading-anchor\" href=\"#geospatial-libraries\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/6.0/ref/contrib/gis/install/geolibs/\">安装地理空间库</a></li>\n</ul>\n</div>\n</section>\n<section id=\"database-installation\">\n<h3>数据库安装<a class=\"heading-anchor\" href=\"#database-installation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/6.0/ref/contrib/gis/install/postgis/\">安装 PostGIS</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/6.0/ref/contrib/gis/install/spatialite/\">安装 SpatiaLite</a></li>\n</ul>\n</div>\n</section>\n<section id=\"databases-configuration\">\n<h3><a class=\"reference internal\" href=\"/zh-hans/6.0/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a> 配置<a class=\"heading-anchor\" href=\"#databases-configuration\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>将 <a class=\"reference internal\" href=\"/zh-hans/6.0/ref/settings/#std-setting-DATABASE-ENGINE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ENGINE</span></code></a> 设置为 <a class=\"reference internal\" href=\"/zh-hans/6.0/ref/contrib/gis/db-api/#spatial-backends\"><span class=\"std std-ref\">空间后端</span></a> 之一。</p>\n</section>\n<section id=\"add-django-contrib-gis-to-installed-apps\">\n<h3>添加 <code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.gis</span></code> 到 <a class=\"reference internal\" href=\"/zh-hans/6.0/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a><a class=\"heading-anchor\" href=\"#add-django-contrib-gis-to-installed-apps\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>和其他 Django contrib 应用一样，你只需要在配置中的 <a class=\"reference internal\" href=\"/zh-hans/6.0/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> 中添加 <a class=\"reference internal\" href=\"/zh-hans/6.0/ref/contrib/gis/#module-django.contrib.gis\" title=\"django.contrib.gis: Geographic Information System (GIS) extensions for Django\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.gis</span></code></a>。这是为了让 <code class=\"docutils literal notranslate\"><span class=\"pre\">gis</span></code> 模板能够被定位 —— 如果不这样做，那么诸如地理管理或 KML 网站地图等功能将无法正常运行。</p>\n</section>\n</section>\n<section id=\"troubleshooting\">\n<h2>错误调试<a class=\"heading-anchor\" href=\"#troubleshooting\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>如果你在这里找不到解决问题的方法，那就参与社区吧！你可以：</p>\n<ul class=\"simple\">\n<li><p>在 <a href=\"#id23\"><span class=\"problematic\" id=\"id25\">`GeoDjango`__</span></a> 论坛上提出您的问题。</p></li>\n<li><p>File a ticket on the <a href=\"#id23\"><span class=\"problematic\" id=\"id26\">`Django trac`__</span></a> if you think there's a bug. Make\nsure to provide a complete description of the problem, versions used,\nand specify the component as &quot;GIS&quot;.</p></li>\n</ul>\n<section id=\"library-environment-settings\">\n<span id=\"libsettings\"></span><h3>库环境配置<a class=\"heading-anchor\" href=\"#library-environment-settings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>到目前为止，安装 GeoDjango 时最常见的问题是无法找到外部共享库（例如 GEOS 和 GDAL）。<a class=\"footnote-reference brackets\" href=\"#id22\" id=\"id5\" role=\"doc-noteref\"><span class=\"fn-bracket\">[</span>1<span class=\"fn-bracket\">]</span></a> 通常情况下，造成这个问题的原因是操作系统不知道从源码建立的库的安装目录。</p>\n<p>一般情况下，库路径可以通过设置环境变量按每个用户来设置，也可以为整个系统配置库路径。</p>\n<section id=\"ld-library-path-environment-variable\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">LD_LIBRARY_PATH</span></code> 环境变量<a class=\"heading-anchor\" href=\"#ld-library-path-environment-variable\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>A user may set this environment variable to customize the library paths\nthey want to use. The typical library directory for software\nbuilt from source is <code class=\"docutils literal notranslate\"><span class=\"pre\">/usr/local/lib</span></code>. Thus, <code class=\"docutils literal notranslate\"><span class=\"pre\">/usr/local/lib</span></code> needs\nto be included in the <code class=\"docutils literal notranslate\"><span class=\"pre\">LD_LIBRARY_PATH</span></code> variable. For example, the user\ncould place the following in their bash profile:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"nb\">export</span><span class=\"w\"> </span><span class=\"nv\">LD_LIBRARY_PATH</span><span class=\"o\">=</span>/usr/local/lib\n</code></pre></div>\n</section>\n<section id=\"setting-system-library-path\">\n<h4>设置系统库路径<a class=\"heading-anchor\" href=\"#setting-system-library-path\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>On GNU/Linux systems, there is typically a file in <code class=\"docutils literal notranslate\"><span class=\"pre\">/etc/ld.so.conf</span></code>, which\nmay include additional paths from files in another directory, such as\n<code class=\"docutils literal notranslate\"><span class=\"pre\">/etc/ld.so.conf.d</span></code>. As the root user, add the custom library path (like\n<code class=\"docutils literal notranslate\"><span class=\"pre\">/usr/local/lib</span></code>) on a new line in <code class=\"docutils literal notranslate\"><span class=\"pre\">ld.so.conf</span></code>. This is <em>one</em> example of\nhow to do so:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>$<span class=\"w\"> </span>sudo<span class=\"w\"> </span><span class=\"nb\">echo</span><span class=\"w\"> </span>/usr/local/lib<span class=\"w\"> </span>&gt;&gt;<span class=\"w\"> </span>/etc/ld.so.conf\n$<span class=\"w\"> </span>sudo<span class=\"w\"> </span>ldconfig\n</code></pre></div>\n<p>For OpenSolaris users, the system library path may be modified using the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">crle</span></code> utility. Run <code class=\"docutils literal notranslate\"><span class=\"pre\">crle</span></code> with no options to see the current configuration\nand use <code class=\"docutils literal notranslate\"><span class=\"pre\">crle</span> <span class=\"pre\">-l</span></code> to set with the new library path. Be <em>very</em> careful when\nmodifying the system library path:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"c1\"># crle -l $OLD_PATH:/usr/local/lib</span>\n</code></pre></div>\n</section>\n<section id=\"install-binutils\">\n<span id=\"binutils\"></span><h4>安装 <code class=\"docutils literal notranslate\"><span class=\"pre\">binutils</span></code><a class=\"heading-anchor\" href=\"#install-binutils\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>GeoDjango uses the <code class=\"docutils literal notranslate\"><span class=\"pre\">find_library</span></code> function (from the <code class=\"docutils literal notranslate\"><span class=\"pre\">ctypes.util</span></code> Python\nmodule) to discover libraries. The <code class=\"docutils literal notranslate\"><span class=\"pre\">find_library</span></code> routine uses a program\ncalled <code class=\"docutils literal notranslate\"><span class=\"pre\">objdump</span></code> (part of the <code class=\"docutils literal notranslate\"><span class=\"pre\">binutils</span></code> package) to verify a shared\nlibrary on GNU/Linux systems. Thus, if <code class=\"docutils literal notranslate\"><span class=\"pre\">binutils</span></code> is not installed on your\nLinux system then Python's ctypes may not be able to find your library even if\nyour library path is set correctly and geospatial libraries were built\nperfectly.</p>\n<p>在 Debian 和 Ubuntu 系统上，可以使用以下命令安装 <code class=\"docutils literal notranslate\"><span class=\"pre\">binutils</span></code> 软件包：</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>$<span class=\"w\"> </span>sudo<span class=\"w\"> </span>apt-get<span class=\"w\"> </span>install<span class=\"w\"> </span>binutils\n</code></pre></div>\n<p>类似地，在 Red Hat 和 CentOS 系统上：</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>$<span class=\"w\"> </span>sudo<span class=\"w\"> </span>yum<span class=\"w\"> </span>install<span class=\"w\"> </span>binutils\n</code></pre></div>\n</section>\n</section>\n</section>\n<section id=\"platform-specific-instructions\">\n<h2>特定平台说明<a class=\"heading-anchor\" href=\"#platform-specific-instructions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"macos\">\n<span id=\"id6\"></span><h3>macOS<a class=\"heading-anchor\" href=\"#macos\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>由于 macOS 的打包系统多种多样，用户在安装 GeoDjango 时有几种不同的选择。这些选择是：</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"#postgresapp\"><span class=\"std std-ref\">Postgres.app</span></a> （最简易且推荐）</p></li>\n<li><p><a class=\"reference internal\" href=\"#homebrew\"><span class=\"std std-ref\">Homebrew</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#fink\"><span class=\"std std-ref\">Fink</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#macports\"><span class=\"std std-ref\">MacPorts</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.0/ref/contrib/gis/install/geolibs/#build-from-source\"><span class=\"std std-ref\">从源码构建</span></a></p></li>\n</ul>\n<p>这一节还包含了从 Python 软件基金会提供的软件包中安装升级版 <a class=\"reference internal\" href=\"#macos-python\"><span class=\"std std-ref\">Python</span></a> 的说明，但是，这不是必须的。</p>\n<section id=\"python\">\n<span id=\"macos-python\"></span><h4>Python<a class=\"heading-anchor\" href=\"#python\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Although macOS comes with Python installed, users can use <a href=\"#id23\"><span class=\"problematic\" id=\"id27\">`framework\ninstallers`__</span></a> provided by the Python Software Foundation. An advantage to\nusing the installer is that macOS's Python will remain &quot;pristine&quot; for internal\noperating system use.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>您需要修改您的 <code class=\"docutils literal notranslate\"><span class=\"pre\">.profile</span></code> 文件中的 <code class=\"docutils literal notranslate\"><span class=\"pre\">PATH</span></code> 环境变量，以便在命令行输入 <code class=\"docutils literal notranslate\"><span class=\"pre\">python</span></code> 时使用新版本的 Python：</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"nb\">export</span><span class=\"w\"> </span><span class=\"nv\">PATH</span><span class=\"o\">=</span>/Library/Frameworks/Python.framework/Versions/Current/bin:<span class=\"nv\">$PATH</span>\n</code></pre></div>\n</aside>\n</section>\n<section id=\"postgres-app\">\n<span id=\"postgresapp\"></span><h4>Postgres.app<a class=\"heading-anchor\" href=\"#postgres-app\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference external\" href=\"https://postgresapp.com/\">Postgres.app</a> is a standalone PostgreSQL server\nthat includes the PostGIS extension. You will also need to install <code class=\"docutils literal notranslate\"><span class=\"pre\">gdal</span></code>\nwith <a class=\"reference internal\" href=\"#homebrew\"><span class=\"std std-ref\">Homebrew</span></a>.</p>\n<p>安装 Postgres.app 后，在你的 <code class=\"docutils literal notranslate\"><span class=\"pre\">.bash_profile</span></code> 中添加以下内容，这样你就可以在命令行中运行软件包的程序。用你安装的 Postgres.app 中的 PostgreSQL 版本替换 <code class=\"docutils literal notranslate\"><span class=\"pre\">X.Y</span></code>：</p>\n<div class=\"code-block\" data-language=\"bash\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"nb\">export</span><span class=\"w\"> </span><span class=\"nv\">PATH</span><span class=\"o\">=</span><span class=\"nv\">$PATH</span>:/Applications/Postgres.app/Contents/Versions/X.Y/bin\n</code></pre></div>\n<p>你可以在终端提示符下输入 <code class=\"docutils literal notranslate\"><span class=\"pre\">which</span> <span class=\"pre\">psql</span></code> 检查路径是否设置正确。</p>\n</section>\n<section id=\"homebrew\">\n<span id=\"id9\"></span><h4>Homebrew<a class=\"heading-anchor\" href=\"#homebrew\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a href=\"#id23\"><span class=\"problematic\" id=\"id28\">`Homebrew`__</span></a> 提供了从源码中构建二进制文件和软件包的 “菜谱”。它为运行 macOS 的 Macintosh 电脑提供了 GeoDjango 的先决条件。因为 Homebrew 仍然从源码构建软件，所以需要使用 <a class=\"reference external\" href=\"https://developer.apple.com/xcode/\">Xcode</a> 。</p>\n<p>摘要：</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>$<span class=\"w\"> </span>brew<span class=\"w\"> </span>install<span class=\"w\"> </span>postgresql\n$<span class=\"w\"> </span>brew<span class=\"w\"> </span>install<span class=\"w\"> </span>postgis\n$<span class=\"w\"> </span>brew<span class=\"w\"> </span>install<span class=\"w\"> </span>gdal\n</code></pre></div>\n<section id=\"fink\">\n<span id=\"id11\"></span><h5>Fink<a class=\"heading-anchor\" href=\"#fink\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p><a href=\"#id23\"><span class=\"problematic\" id=\"id29\">`Kurt Schwehr`__</span></a> 很慷慨地为 <a href=\"#id23\"><span class=\"problematic\" id=\"id30\">`Fink`__</span></a> 包系统的用户创建了 GeoDjango 包。根据你想使用的 Python 版本，可以使用不同的包（从 <code class=\"docutils literal notranslate\"><span class=\"pre\">django-gis</span></code> 开始）。</p>\n</section>\n</section>\n<section id=\"macports\">\n<span id=\"id15\"></span><h4>MacPorts<a class=\"heading-anchor\" href=\"#macports\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a href=\"#id23\"><span class=\"problematic\" id=\"id31\">`MacPorts`__</span></a> may be used to install GeoDjango prerequisites on computers\nrunning macOS. Because MacPorts still builds the software from source,\n<a class=\"reference external\" href=\"https://developer.apple.com/xcode/\">Xcode</a> is required.</p>\n<p>摘要：</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>$<span class=\"w\"> </span>sudo<span class=\"w\"> </span>port<span class=\"w\"> </span>install<span class=\"w\"> </span>postgresql14-server\n$<span class=\"w\"> </span>sudo<span class=\"w\"> </span>port<span class=\"w\"> </span>install<span class=\"w\"> </span>geos\n$<span class=\"w\"> </span>sudo<span class=\"w\"> </span>port<span class=\"w\"> </span>install<span class=\"w\"> </span>proj6\n$<span class=\"w\"> </span>sudo<span class=\"w\"> </span>port<span class=\"w\"> </span>install<span class=\"w\"> </span>postgis3\n$<span class=\"w\"> </span>sudo<span class=\"w\"> </span>port<span class=\"w\"> </span>install<span class=\"w\"> </span>gdal\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>您还需要在您的 <code class=\"docutils literal notranslate\"><span class=\"pre\">.profile</span></code> 中修改 <code class=\"docutils literal notranslate\"><span class=\"pre\">PATH</span></code>，以便从命令行访问 MacPorts 程序：</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"nb\">export</span><span class=\"w\"> </span><span class=\"nv\">PATH</span><span class=\"o\">=</span>/opt/local/bin:/opt/local/lib/postgresql14/bin\n</code></pre></div>\n<p>此外，添加 <code class=\"docutils literal notranslate\"><span class=\"pre\">DYLD_FALLBACK_LIBRARY_PATH</span></code> 设置以便 Python 可以找到这些库：</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"nb\">export</span><span class=\"w\"> </span><span class=\"nv\">DYLD_FALLBACK_LIBRARY_PATH</span><span class=\"o\">=</span>/opt/local/lib:/opt/local/lib/postgresql14\n</code></pre></div>\n</aside>\n</section>\n</section>\n<section id=\"windows\">\n<span id=\"id17\"></span><h3>Windows<a class=\"heading-anchor\" href=\"#windows\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>为了在 Windows 上安装 GeoDjango，请依次完成以下章节。在本教程中，我们将安装每个应用程序的 64 位版本。</p>\n<section id=\"id18\">\n<h4>Python<a class=\"heading-anchor\" href=\"#id18\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>安装一个 64 位版本的 Python。更多信息请参见 <a class=\"reference internal\" href=\"/zh-hans/6.0/howto/windows/\"><span class=\"doc\">安装 Python</span></a>。</p>\n</section>\n<section id=\"postgresql\">\n<h4>PostgreSQL<a class=\"heading-anchor\" href=\"#postgresql\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Download the latest <a href=\"#id23\"><span class=\"problematic\" id=\"id32\">`PostgreSQL 15.x installer`__</span></a> from the\n<a href=\"#id23\"><span class=\"problematic\" id=\"id33\">`EnterpriseDB`__</span></a> website. After downloading, run the installer, follow the\non-screen directions, and keep the default options unless you know the\nconsequences of changing them.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>PostgreSQL 安装程序会创建一个新的 <code class=\"docutils literal notranslate\"><span class=\"pre\">postgres</span></code> 数据库超级用户，你会被提示设置一次密码 —— 一定要记住它！</p>\n</aside>\n<p>当安装程序完成时，它会询问 “在退出时启动 Stack Builder?” —— 请确定这个问题，因为需要安装 <a class=\"reference internal\" href=\"#postgisasb\"><span class=\"std std-ref\">PostGIS</span></a>。</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>If installed successfully, the PostgreSQL server will run in the background\neach time the system is started as a Windows service. A\n<span class=\"menuselection\">PostgreSQL 15</span> start menu group will created and contains\nshortcuts for the Application Stack Builder (ASB) as well as the\n'SQL Shell', which will launch a <code class=\"docutils literal notranslate\"><span class=\"pre\">psql</span></code> command window.</p>\n</aside>\n</section>\n<section id=\"postgis\">\n<span id=\"postgisasb\"></span><h4>PostGIS<a class=\"heading-anchor\" href=\"#postgis\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>在 Stack Builder 中（在安装程序外运行，选择 <span class=\"menuselection\">开始 ‣ PostgreSQL 15 ‣ Application Stack Builder</span>），从下拉菜单中选择 <span class=\"menuselection\">PostgreSQL 15 (x64) on port 5432</span>，然后点击下一步。展开 <span class=\"menuselection\">类别 ‣ 空间扩展</span> 菜单树，并选择 <span class=\"menuselection\">PostGIS X.Y for PostgreSQL 15</span>。</p>\n<p>点击下一步后，您将被提示确认所选的包和“下载目录”。再次点击下一步，这将下载 PostGIS，并要求您点击下一步开始 PostGIS 安装程序。在安装过程中，包括四个 Yes/No 对话框，所有四个的默认选项都是 &quot;No&quot;。</p>\n</section>\n<section id=\"osgeo4w\">\n<span id=\"id21\"></span><h4>OSGeo4W<a class=\"heading-anchor\" href=\"#osgeo4w\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The <a class=\"reference external\" href=\"https://trac.osgeo.org/osgeo4w/\">OSGeo4W installer</a> helps to install the PROJ, GDAL, and GEOS libraries\nrequired by GeoDjango. First, download the <a class=\"reference external\" href=\"https://trac.osgeo.org/osgeo4w/\">OSGeo4W installer</a>, and\nrun it. Select <span class=\"menuselection\">Express Web-GIS Install</span> and click next. In the\n'Select Packages' list, ensure that GDAL is selected. If any other packages are\nenabled by default, they are not required by GeoDjango and may be unchecked\nsafely. After clicking next and accepting the license agreements, the packages\nwill be automatically downloaded and installed, after which you may exit the\ninstaller.</p>\n</section>\n<section id=\"modify-windows-environment\">\n<h4>修改 Windows 环境<a class=\"heading-anchor\" href=\"#modify-windows-environment\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>In order to use GeoDjango, you will need to add your OSGeo4W\ndirectories to your Windows system <code class=\"docutils literal notranslate\"><span class=\"pre\">Path</span></code>, as well as create <code class=\"docutils literal notranslate\"><span class=\"pre\">GDAL_DATA</span></code>\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">PROJ_LIB</span></code> environment variables. The following set of commands,\nexecutable with <code class=\"docutils literal notranslate\"><span class=\"pre\">cmd.exe</span></code>, will set this up. Restart your device\nonce this is complete for new environment variables to be recognized:</p>\n<div class=\"code-block\" data-language=\"bat\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Bat</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Bat code\"><code><span class=\"k\">set</span> <span class=\"nv\">OSGEO4W_ROOT</span><span class=\"p\">=</span>C:\\OSGeo4W\n<span class=\"k\">set</span> <span class=\"nv\">GDAL_DATA</span><span class=\"p\">=</span><span class=\"nv\">%OSGEO4W_ROOT%</span>\\apps\\gdal\\share\\gdal\n<span class=\"k\">set</span> <span class=\"nv\">PROJ_LIB</span><span class=\"p\">=</span><span class=\"nv\">%OSGEO4W_ROOT%</span>\\share\\proj\n<span class=\"k\">set</span> <span class=\"nv\">PATH</span><span class=\"p\">=</span><span class=\"nv\">%PATH%</span>;<span class=\"nv\">%OSGEO4W_ROOT%</span>\\bin\nreg ADD <span class=\"s2\">&quot;HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment&quot;</span> /v Path /t REG_EXPAND_SZ /f /d <span class=\"s2\">&quot;</span><span class=\"nv\">%PATH%</span><span class=\"s2\">&quot;</span>\nreg ADD <span class=\"s2\">&quot;HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment&quot;</span> /v GDAL_DATA /t REG_EXPAND_SZ /f /d <span class=\"s2\">&quot;</span><span class=\"nv\">%GDAL_DATA%</span><span class=\"s2\">&quot;</span>\nreg ADD <span class=\"s2\">&quot;HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment&quot;</span> /v PROJ_LIB /t REG_EXPAND_SZ /f /d <span class=\"s2\">&quot;</span><span class=\"nv\">%PROJ_LIB%</span><span class=\"s2\">&quot;</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>执行这些命令需要管理员权限。要做到这一点，请以管理员身份运行命令提示符，并输入上述命令。你需要注销并重新登录，设置才会生效。</p>\n</aside>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>如果你自定义了 OSGeo4W 的安装目录，那么你需要相应地修改 <code class=\"docutils literal notranslate\"><span class=\"pre\">OSGEO4W_ROOT</span></code> 变量。</p>\n</aside>\n</section>\n<section id=\"install-django-and-set-up-database\">\n<h4>安装 Django 并建立数据库<a class=\"heading-anchor\" href=\"#install-django-and-set-up-database\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>在您的系统上 <a class=\"reference internal\" href=\"/zh-hans/6.0/topics/install/#installing-official-release\"><span class=\"std std-ref\">安装 Django</span></a>。建议为您创建的每个项目创建一个 <a class=\"reference external\" href=\"https://docs.python.org/3/tutorial/venv.html\" title=\"(in Python v3.14)\"><span class=\"xref std std-doc\">虚拟环境</span></a>。</p>\n</section>\n<section id=\"psycopg\">\n<h4>psycopg<a class=\"heading-anchor\" href=\"#psycopg\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">psycopg</span></code> Python 模块提供了 Python 和 PostgreSQL 数据库之间的接口。您可以在 Python 虚拟环境中使用 pip 安装 <code class=\"docutils literal notranslate\"><span class=\"pre\">psycopg</span></code>：</p>\n<div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows code\"><code><span class=\"gp\">...\\&gt;</span> py -m pip install psycopg\n</code></pre></div>\n<p class=\"rubric\">脚注</p>\n<aside class=\"footnote-list brackets\">\n<aside class=\"footnote brackets\" id=\"id22\" role=\"doc-footnote\">\n<span class=\"label\"><span class=\"fn-bracket\">[</span><a role=\"doc-backlink\" href=\"#id5\">1</a><span class=\"fn-bracket\">]</span></span>\n<p>GeoDjango 使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">ctypes.util</span></code> 中的 <a class=\"reference external\" href=\"https://docs.python.org/3/library/ctypes.html#ctypes.util.find_library\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">find_library()</span></code></a> 例程来查找共享库。</p>\n</aside>\n</aside>\n</section>\n</section>\n</section>","rootId":"geodjango-installation","toc":[{"title":"概况","anchor":"overview","children":[]},{"title":"依赖项","anchor":"requirements","children":[{"title":"Python 和 Django","anchor":"python-and-django","children":[]},{"title":"空间数据库","anchor":"spatial-database","children":[]}]},{"title":"安装","anchor":"installation","children":[{"title":"地理空间库","anchor":"geospatial-libraries","children":[]},{"title":"数据库安装","anchor":"database-installation","children":[]},{"title":"DATABASES 配置","anchor":"databases-configuration","children":[]},{"title":"添加 django.contrib.gis 到 INSTALLED_APPS","anchor":"add-django-contrib-gis-to-installed-apps","children":[]}]},{"title":"错误调试","anchor":"troubleshooting","children":[{"title":"库环境配置","anchor":"library-environment-settings","children":[{"title":"LD_LIBRARY_PATH 环境变量","anchor":"ld-library-path-environment-variable","children":[]},{"title":"设置系统库路径","anchor":"setting-system-library-path","children":[]},{"title":"安装 binutils","anchor":"install-binutils","children":[]}]}]},{"title":"特定平台说明","anchor":"platform-specific-instructions","children":[{"title":"macOS","anchor":"macos","children":[{"title":"Python","anchor":"python","children":[]},{"title":"Postgres.app","anchor":"postgres-app","children":[]},{"title":"Homebrew","anchor":"homebrew","children":[{"title":"Fink","anchor":"fink","children":[]}]},{"title":"MacPorts","anchor":"macports","children":[]}]},{"title":"Windows","anchor":"windows","children":[{"title":"Python","anchor":"id18","children":[]},{"title":"PostgreSQL","anchor":"postgresql","children":[]},{"title":"PostGIS","anchor":"postgis","children":[]},{"title":"OSGeo4W","anchor":"osgeo4w","children":[]},{"title":"修改 Windows 环境","anchor":"modify-windows-environment","children":[]},{"title":"安装 Django 并建立数据库","anchor":"install-django-and-set-up-database","children":[]},{"title":"psycopg","anchor":"psycopg","children":[]}]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API 参考","url":"/zh-hans/6.0/ref/"},{"docname":"ref/contrib/index","title":"contrib 包","url":"/zh-hans/6.0/ref/contrib/"},{"docname":"ref/contrib/gis/index","title":"GeoDjango","url":"/zh-hans/6.0/ref/contrib/gis/"}],"prev":{"docname":"ref/contrib/gis/tutorial","title":"GeoDjango 教程","url":"/zh-hans/6.0/ref/contrib/gis/tutorial/"},"next":{"docname":"ref/contrib/gis/install/geolibs","title":"安装地理空间库","url":"/zh-hans/6.0/ref/contrib/gis/install/geolibs/"},"formats":{"html":"/zh-hans/6.0/ref/contrib/gis/install/","markdown":"/zh-hans/6.0/ref/contrib/gis/install.md","json":"/zh-hans/6.0/ref/contrib/gis/install.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/ref/contrib/gis/install/index.txt","official":"https://docs.djangoproject.com/zh-hans/6.0/ref/contrib/gis/install/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}