Windows 上的 GeoDjango:尝试在设置中设置 GDAL_LIBRARY_PATH

问题描述 投票:0回答:11

我以前已经这样做过十几次了,但是这次有些东西不起作用..

遵循文档:

https://docs.djangoproject.com/en/1.11/ref/contrib/gis/install/#windows

我正在尝试在 Windows 计算机上设置 GeoDjango(这是在 paperspace.com 上设置的虚拟 Windows 10)。我的 PATH 设置似乎有问题,但我无法弄清楚它是什么。我已经运行了说明中突出显示的命令。我检查了我的 PATH 变量,一切似乎都正常。我尝试向他们指出 OSGeo4Win 的 32 位和 64 位版本。无论如何,我每次都会得到以下输出:

C:\Python\lib\site-packages\floppyforms\__init__.py:21: UserWarning: Unable to import floppyforms.gis, geometry widgets not available
  "Unable to import floppyforms.gis, geometry widgets not available")
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "C:\Python\lib\site-packages\django\core\management\__init__.py", line 337, in execute
    django.setup()
  File "C:\Python\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Python\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models()
  File "C:\Python\lib\site-packages\django\apps\config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "C:\Python\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\Python\lib\site-packages\django\contrib\auth\base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\Python\lib\site-packages\django\db\models\base.py", line 124, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "C:\Python\lib\site-packages\django\db\models\base.py", line 330, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Python\lib\site-packages\django\db\models\options.py", line 214, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "C:\Python\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "C:\Python\lib\site-packages\django\db\utils.py", line 211, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "C:\Python\lib\site-packages\django\db\utils.py", line 115, in load_backend
    return import_module('%s.base' % backend_name)
  File "C:\Python\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "C:\Python\lib\site-packages\django\contrib\gis\db\backends\postgis\base.py", line 5, in <module>
    from .features import DatabaseFeatures
  File "C:\Python\lib\site-packages\django\contrib\gis\db\backends\postgis\features.py", line 1, in <module>
    from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures
  File "C:\Python\lib\site-packages\django\contrib\gis\db\backends\base\features.py", line 4, in <module>
    from django.contrib.gis.db.models import aggregates
  File "C:\Python\lib\site-packages\django\contrib\gis\db\models\__init__.py", line 3, in <module>
    from django.contrib.gis.db.models.aggregates import *  # NOQA
  File "C:\Python\lib\site-packages\django\contrib\gis\db\models\aggregates.py", line 1, in <module>
    from django.contrib.gis.db.models.fields import ExtentField
  File "C:\Python\lib\site-packages\django\contrib\gis\db\models\fields.py", line 3, in <module>
    from django.contrib.gis import forms, gdal
  File "C:\Python\lib\site-packages\django\contrib\gis\forms\__init__.py", line 3, in <module>
    from .fields import (  # NOQA
  File "C:\Python\lib\site-packages\django\contrib\gis\forms\fields.py", line 4, in <module>
    from django.contrib.gis.geos import GEOSException, GEOSGeometry
  File "C:\Python\lib\site-packages\django\contrib\gis\geos\__init__.py", line 5, in <module>
    from .collections import (  # NOQA
  File "C:\Python\lib\site-packages\django\contrib\gis\geos\collections.py", line 11, in <module>
    from django.contrib.gis.geos.geometry import GEOSGeometry, LinearGeometryMixin
  File "C:\Python\lib\site-packages\django\contrib\gis\geos\geometry.py", line 11, in <module>
    from django.contrib.gis import gdal
  File "C:\Python\lib\site-packages\django\contrib\gis\gdal\__init__.py", line 28, in <module>
    from django.contrib.gis.gdal.datasource import DataSource
  File "C:\Python\lib\site-packages\django\contrib\gis\gdal\datasource.py", line 39, in <module>
    from django.contrib.gis.gdal.driver import Driver
  File "C:\Python\lib\site-packages\django\contrib\gis\gdal\driver.py", line 5, in <module>
    from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
  File "C:\Python\lib\site-packages\django\contrib\gis\gdal\prototypes\ds.py", line 9, in <module>
    from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
  File "C:\Python\lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 44, in <module>
    'GDAL_LIBRARY_PATH in your settings.' % '", "'.join(lib_names)
django.contrib.gis.gdal.error.GDALException: Could not find the GDAL library (tried "gdal111", "gdal110", "gdal19", "gdal18", "gdal17"). Try setting GDAL_LIBRARY_PATH in your settings.

有人有什么想法吗?

更新: 我去商店买了一台新笔记本电脑,但仍然遇到同样的问题。这可能是某些 GeoDjango 依赖项的版本控制问题吗?我不知道该怎么办,但这对我来说有点紧急。

python django windows gdal geodjango
11个回答
24
投票

问题最终是由于 Django 和 GDAL 之间的版本不匹配。 Django 没有搜索正确的文件名(在我的例子中是

gdal202.dll
)。

修复它需要我将

str('gdal202')
添加到以下文件的第 26 行:

(Python Root)\Lib\site-packages\django\contrib\gis\gdal\libgdal.py

请注意,如果您在虚拟环境中工作,那么

Python Root
实际上将位于任何位置,即:
Users\YourName\Envs\project

如果此问题再次出现,您可以查看

C:\OSGeo4W\bin
目录以找出 Django 需要搜索哪个
gdalxxx.dll

此外,请确保您使用的是32位版本的Python和OSGeo4Win。否则,您可能会看到同样的失败。


19
投票

添加

GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal202'

对我有用的 django 设置


4
投票

除了之前的评论。您可能会遇到[WinError 127]无法找到指定的过程。

出现这种情况是因为您的系统上有多个 sqlite3.dll。如果路径首先命中这些路径,则会出现 gdal202 冲突。

您可以通过添加工作目录的更改来扩展 Adam Starr 的答案。

#Set working directory to actual working directoy of gdal
#This is required to prevent any conflicts with older SQLITE versions, 
#f.e. in python path  
#Prevents error in GDAL 202
#BASE_WORKINGDIRECTORY to change it back later if you need to
BASE_WORKDIRECTORY = os.getcwd()
os.chdir(os.path.dirname(lib_path))

# This loads the GDAL/OGR C library
lgdal = CDLL(lib_path)

3
投票

我遇到了同样的错误。

django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal204", "gdal203", "gdal202", "gdal201", "gdal20"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.

首先,安装OSGeo4W ,然后在你的settings.py中添加以下代码。

import os
if os.name == 'nt':
    import platform
    OSGEO4W = r"C:\OSGeo4W"
    if '64' in platform.architecture()[0]:
        OSGEO4W += "64"
    assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W
    os.environ['OSGEO4W_ROOT'] = OSGEO4W
    os.environ['GDAL_DATA'] = OSGEO4W + r"\share\gdal"
    os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
    os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']

然后运行

python manage.py check
如果仍然出现错误,请转到 C:\OSGeo4W64 或 C:\OSGeo4W。 在那里你可以找到gdalxxx.dll。请将错误消息中的文件名重命名为“gdal202”或“gdal203”。 请运行
python manage.py check
,这一定有效。


3
投票

我在虚拟环境中的 Django 中也遇到了同样的问题。解决我的问题的是:

  1. 在settings.py中添加您的应用程序尝试使用的gdalxxx.dll的路径,对我来说是

     GDAL_LIBRARY_PATH = r'C:\Users\<User>\Documents\<proj>\Lib\site-packages\osgeo\gdal301'
    

仅此一项就可能解决您的问题,但 Django 可能仍然会抱怨缺少 GEOS 库,在这种情况下:

  1. 在settings.py中添加您的应用程序尝试使用的geos_c.dll的路径,对我来说是:

    GEOS_LIBRARY_PATH = r'C:\Users\<User>\Documents\<proj>\Lib\site-packages\shapely\DLLs\geos_c'
    

2
投票

如果您使用 Django 教程并通过 OSGeo4W 安装 GDAL,则在修改 Windows 环境时,请确保您已定义正确的 Python 和 OSGeo4W 版本。

例如安装64位OSGeo4W时,需要设置环境路径:

set OSGEO4W_ROOT=C:\OSGeo4W64

对于 Python 3.5.2:

set PYTHON_ROOT=C:\Python36

确保您没有将 virtualenv 中的 Python 安装与系统 Python 混淆。

仅在正确修改环境后安装Django。


1
投票
对我来说,这个问题在

Windows

 上得到了解决,只需将 
osgeo
 python 包的路径添加到环境变量(C:\Python36\Lib\site-packages\osgeo)即可。看来这样做之后,django 设法找到了 
gdal202.dll
 文件。


0
投票
解决方案:

    从 pip 安装 GDAL:
  1. pip install GDAL
    ,如果不起作用请安装
    来自轮子:
    https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal
  2. a) 如果您使用虚拟环境,只需将此代码添加到

    settings.pyos.environ["PATH"] += os.pathsep + BASE_DIR + '\\venv\\Lib\\site-packages\\osgeo'

    
    
    

    b) 否则添加此代码:

    from distutils.sysconfig import get_python_lib os.environ["PATH"] += os.pathsep + get_python_lib() + '\\osgeo'


0
投票
makemigrations时,出现这样的问题

在此输入图片描述

也许 dll(gdal202.dll) 库不起作用。 所以我们可以用 GDAL-whl-package 替换整个 GDAL lib

https://download.lfd.uci.edu/pythonlibs/r5uhg2lo/GDAL-2.3.2-cp36-cp36m-win_amd64.whl 下载此whl包,将其重命名为GDAL-2.3.2-cp36-cp36m-win_amd64.whl.zip 在此输入图片描述 将 osgeo 文件夹解压到一处 (D:\ProgramData\osgeo) 并修改 GDAL_DATA PROJ_LIB 及其子文件夹的路径

意味着改变Windows环境 来自

set OSGEO4W_ROOT=C:\OSGeo4W set PYTHON_ROOT=C:\Python3X set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal set PROJ_LIB=%OSGEO4W_ROOT%\share\proj set PATH=%PATH%;%PYTHON_ROOT%;%OSGEO4W_ROOT%\bin reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /f /d "%PATH%" reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v GDAL_DATA /t REG_EXPAND_SZ /f /d "%GDAL_DATA%" reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROJ_LIB /t REG_EXPAND_SZ /f /d "%PROJ_LIB%"

set OSGEO4W_ROOT=D:\ProgramData\osgeo set PYTHON_ROOT=C:\Python3X set GDAL_DATA=%OSGEO4W_ROOT%\data\gdal set PROJ_LIB=%OSGEO4W_ROOT%\data\proj set PATH=%PATH%;%PYTHON_ROOT%;%OSGEO4W_ROOT%\bin reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /f /d "%PATH%" reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v GDAL_DATA /t REG_EXPAND_SZ /f /d "%GDAL_DATA%" reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROJ_LIB /t REG_EXPAND_SZ /f /d "%PROJ_LIB%"

PS : 不要忘记将“D:\ProgramData\osgeo\gdal203.dll”重命名为“D:\ProgramData\osgeo\gdal202.dll”


0
投票
问题已通过应用此

链接中的修复程序得到解决


0
投票
我在 Windows 中遇到了同样的问题,在 python 3.12 中安装 gdal 所以首先我安装了软件 Qgis 并在 django 设置中添加了

GDAL_LIBRARY_PATH = r'C:\Program Files\QGIS 3.36.1\bin\gdal308.dll' GEOS_LIBRARY_PATH = r'C:\Program Files\QGIS 3.36.1\bin\geos_c.dll'
    
© www.soinside.com 2019 - 2024. All rights reserved.