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

时间:2017-05-23 16:23:07

标签: python django windows gdal geodjango

我之前已经做了十几次,但这次有些事情没有用。

关注文档:

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

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

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依赖项的版本问题吗?我不知道该怎么办,但对我来说这是一种紧急情况。

9 个答案:

答案 0 :(得分:10)

该问题最终成为version mismatch between Django and GDAL。 Django没有搜索正确的文件名(在我的例子中是gdal202.dll)。

修复它需要我在第26行添加str('gdal202')到以下文件:

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

如果再次出现此问题,您可以查看OSGeo4W\bin目录,找出Django需要搜索的gdalxxx.dll

答案 1 :(得分:3)

添加

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

到为我工作的django设置

答案 2 :(得分:3)

除了之前的评论。您可能有[WinError 127]找不到指定的过程。

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

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

#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 :(得分:2)

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

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

set OSGEO4W_ROOT=C:\OSGeo4W64

和Python 3.5.2:

set PYTHON_ROOT=C:\Python36

确保您不会在使用系统Python的virtualenv中混淆Python安装。

只有在之后安装了Django 才能正确修改环境。

答案 4 :(得分:1)

对我来说,只需将Windows python包的路径添加到Environment变量(C:\ Python36 \ Lib \ site-packages \ osgeo),就可以在osgeo上解决此问题。似乎在执行此操作后,django设法找到gdal202.dll文件。

答案 5 :(得分:1)

我遇到了同样的错误。

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,那必须可行。

答案 6 :(得分:0)

解决方案:

  1. 从pip pip install GDAL安装GDAL,如果不起作用,请安装 从转盘:https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal
  2. a)如果使用虚拟环境,只需将此代码添加到 settings.py 中: os.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'

答案 7 :(得分:0)

当进行makemigretions时,出现这样的问题 enter image description here

也许dll(gdal202.dll)库不起作用。 因此我们可以用https://download.lfd.uci.edu/pythonlibs/r5uhg2lo/GDAL-2.3.2-cp36-cp36m-win_amd64.whl中的GDAL-whl-package替换整个GDAL库 下载此Whl软件包,将其重命名为GDAL-2.3.2-cp36-cp36m-win_amd64.whl.zip enter image description here 将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”

答案 8 :(得分:0)

在虚拟环境中的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'