postgresql Windows 上的 GeoDjango:“找不到 GDAL 库”/“OSError:[WinError 126] 找不到指定的模块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49139044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:41:50  来源:igfitidea点击:

GeoDjango on Windows: "Could not find the GDAL library" / "OSError: [WinError 126] The specified module could not be found"

pythondjangopostgresqlpostgisgeodjango

提问by Yorian

I've been trying to setup my windows computer such that I can have a local postgreSQL with PostGIS extension. With this installed I hope to be able to create a project with geodjango locally before putting it in the cloud. I have been working with Django for a little while now on my local machine with the SQLite DB, but since the next project will partly be based on coordinate based data I wanted to setup the right environment.

我一直在尝试设置我的 Windows 计算机,以便我可以拥有带有 PostGIS 扩展的本地 postgreSQL。安装了这个之后,我希望能够在将它放入云之前在本地创建一个带有 geodjango 的项目。我已经在我的本地机器上使用 Django 使用 SQLite DB 一段时间了,但由于下一个项目将部分基于基于坐标的数据,我想设置正确的环境。

Import note: I've installed mini-conda to run in a seperate environment. I do activate this environment "development" when I work though

导入说明:我已经安装了 mini-conda 以在单独的环境中运行。我在工作时确实激活了这个环境“发展”

I've tried to follow most of the geodjango information/tutorials online, but can't get it to work. What I've done (mostly followed this: https://docs.djangoproject.com/en/2.0/ref/contrib/gis/install/#windows):

我已经尝试在线关注大部分 geodjango 信息/教程,但无法使其正常工作。我所做的(主要是这样的:https: //docs.djangoproject.com/en/2.0/ref/contrib/gis/install/#windows):

  1. Download and install the latest (10.3) PostgreSQL setup from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
  2. After installation I also installed used the Application Stack Builder to install PostGis
  3. I've installed OSGeo4W from https://trac.osgeo.org/osgeo4w/
  4. I've created a batch script as described on the geodjango website (https://docs.djangoproject.com/en/2.0/ref/contrib/gis/install/#windows) and ran it as administrator (except for the part where it sets the path to python, cause python was already in there since I've been using python for a while now)
  5. I've tried some command in psql shell and I think I've created a database with name: geodjango, username: **** and pass: ****.
  6. I don't know if I have given the geodjango user all priveleges, but I suspect so.
  1. https://www.enterprisedb.com/downloads/postgres-postgresql-downloads下载并安装最新的 (10.3) PostgreSQL 设置
  2. 安装后我还安装了使用 Application Stack Builder 来安装 PostGis
  3. 我已经从https://trac.osgeo.org/osgeo4w/安装了 OSGeo4W
  4. 我已经按照 geodjango 网站(https://docs.djangoproject.com/en/2.0/ref/contrib/gis/install/#windows)上的描述创建了一个批处理脚本并以管理员身份运行它(除了它设置了python的路径,因为python已经在那里了,因为我已经使用python一段时间了)
  5. 我在 psql shell 中尝试了一些命令,我​​想我已经创建了一个名为:geodjango,用户名:**** 和 pass:**** 的数据库。
  6. 我不知道我是否给了 geodjango 用户所有的特权,但我怀疑是这样。

After all of this I created a new django project and in settings.py I've added some parts:

在这一切之后,我创建了一个新的 django 项目,并在 settings.py 中添加了一些部分:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'nameOfMyApp',
]

I've also got this in settings.py:

我也在 settings.py 中得到了这个:

DATABASES = {
'default': {
    'ENGINE': 'django.contrib.gis.db.backends.postgis',
    'NAME': 'geodjango',
    'USER': '****',
    'PASSWORD': '****',
    'HOST': 'localhost',
}
}

# FOR GEODJANGO
POSTGIS_VERSION = (2, 4, 3)

When I try to set up the database in django I run (in the right folder):

当我尝试在 Django 中设置数据库时,我运行(在正确的文件夹中):

python manage.py makemigrations

I get the following error:

我收到以下错误:

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

I've tried to fix that, but nothing seems to work. Can anybody give me some help in setting this all up locally?

我试图解决这个问题,但似乎没有任何效果。有人可以帮我在本地设置这一切吗?

Update 7-3-2018:

2018 年 7 月 3 日更新:

Now I get the following error:

现在我收到以下错误:

OSError: [WinError 126] The specified module could not be found

(while the .dll is there...)

(虽然 .dll 在那里......)

回答by Udi

I have found the following to work for windows:

我发现以下内容适用于 Windows:

  • Run pythonto check if your pythonis 32 or 64 bit.
  • Install corresponding OSGeo4W(32 or 64 bit) into C:\OSGeo4Wor C:\OSGeo4W64:
    • Note:Select Express Web-GIS Install and click next.
    • In the ‘Select Packages' list, ensure that GDAL is selected; MapServer and Apache are also enabled by default, may be unchecked safely.
  • Make sure the following is included in your 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']
    
  • Run python manage.py checkto verify geodjango is working correctly.

  • 运行python以检查您的python是 32 位还是 64 位。
  • 将相应的OSGeo4W(32 或 64 位)安装到C:\OSGeo4WC:\OSGeo4W64
    • 注意:选择 Express Web-GIS 安装并单击下一步。
    • 在“选择包”列表中,确保选择了 GDAL;MapServer 和 Apache 也默认启用,可以安全地取消选中。
  • 确保以下内容包含在您的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以验证 geodjango 是否正常工作。

回答by keepAlive

In my case (Windows10Pro+Python3.7.1), having the (automatically chosen) dll present was not enough, namely gdal111.dll.

在我的情况(Windows10Pro+Python3.7.1) 中,存在(自动选择的)dll 是不够的,即gdal111.dll.

I realized that I also had gdal204.dlllocated at C:\OSGeo4W\binand tried to "enrich" the list variable named lib_nameswith 'gdal204', at line 24 (regarding Windows NT shared libraries) of %PYTHON_ROOT%\Lib\site-packages\django\contrib\gis\gdal\libgdal.py, i.e.

我意识到,我也已经gdal204.dll位于C:\OSGeo4W\bin并试图“充实”评选的列表变量lib_names'gdal204'在第24行(对于Windows NT的共享库)的%PYTHON_ROOT%\Lib\site-packages\django\contrib\gis\gdal\libgdal.py,即

#[...]
elif os.name == 'nt':
    # Windows NT shared libraries
    lib_names = ['gdal204', 'gdal202', 'gdal201', 'gdal20', 'gdal111', 'gdal110', 'gdal19']
#[...]            ^^^^^^^

No negative consequences for now.

目前没有负面影响。

回答by user42121

For Microsoft Windows 10 & Python3.6.8, I installed GDAL 2.3.3 from Unofficial Windows Binaries for Python Extension Packages, modified libgdal.py adding gdal203in the lib_names list env\Lib\site-packages\django\contrib\gis\gdal\libgdal.py.

对于 Microsoft Windows 10 和 Python3.6.8,我从Unofficial Windows Binaries for Python Extension Packages安装了 GDAL 2.3.3 ,修改了 libgdal.py 添加gdal203到 lib_names 列表中env\Lib\site-packages\django\contrib\gis\gdal\libgdal.py

Finally, added osgeo and proj to the PATH, and set the GDAL_LIBRARY_PATH as below (beginning of settings.py):

最后,将 osgeo 和 proj 添加到 PATH,并设置 GDAL_LIBRARY_PATH 如下(settings.py 的开头):

os.environ['PATH'] = os.path.join(BASE_DIR, r'env\Lib\site-packages\osgeo') + ';' + os.environ['PATH']

os.environ['PROJ_LIB'] = os.path.join(BASE_DIR, r'env3\Lib\site-packages\osgeo\data\proj') + ';' + os.environ['PATH']

GDAL_LIBRARY_PATH = os.path.join(BASE_DIR, r'env\Lib\site-packages\osgeo\gdal203.dll') 

In this case, env is my Python environment.

在这种情况下, env 是我的 Python 环境。

回答by karthik k

Steps to follow:

要遵循的步骤:

  1. Run python to check if your python is 32 or 64 bit.
  2. Install corresponding OSGeo4W (32 or 64 bit) into C:\OSGeo4Wor C:\OSGeo4W64:
    Note: Select Express Web-GIS Install and click next.
  3. In the ‘Select Packages' list, ensure that GDAL is selected; MapServer and Apache are also enabled by default.
  4. Make sure the following is included in your settings.py:
  1. 运行 python 检查你的 python 是 32 位还是 64 位。
  2. 将相应的 OSGeo4W(32 或 64 位)安装到C:\OSGeo4W或 中C:\OSGeo4W64
    注意:选择 Express Web-GIS 安装并单击下一步。
  3. 在“选择包”列表中,确保选择了 GDAL;MapServer 和 Apache 也默认启用。
  4. 确保以下内容包含在您的settings.py:
import os
GDAL_LIBRARY_PATH = r'C:\OSGeo4W\bin\gdal300'
  1. Now, run the server still if it doesn't work. Run the following commands in terminal.
  1. 现在,如果它不起作用,请继续运行服务器。在终端中运行以下命令。
set OSGEO4W_ROOT=C:\OSGeo4W
set PYTHON_ROOT=C:\Python3X 
set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal 
set PROJ_LIB=%OSGEO4W_ROOT%\share\proj

回答by kingurr

After updating some OSGEO4W on my Windows 10 Pro machine I started having problems with the GDAL bindings again. I previously used a combination of the solutions posted here.

在我的 Windows 10 Pro 机器上更新了一些 OSGEO4W 后,我又开始遇到 GDAL 绑定问题。我以前使用了此处发布的解决方案的组合。

This is what works for me using Windows 10 Pro 64-bit, Django 3.0.6 and GDAL 3.0.4 using a python 3.7 virtual environment. I have tested it without OSGEO4W and it seems to work.

这对我使用 Windows 10 Pro 64 位、Django 3.0.6 和 GDAL 3.0.4 使用 python 3.7 虚拟环境有效。我已经在没有 OSGEO4W 的情况下对其进行了测试,它似乎可以工作。

First, download the GDAL wheel from Christoph Gohlke's Unofficial Windows Binaries for Python Extension Packages.

首先,从Christoph Gohlke 的 Unofficial Windows Binaries for Python Extension Packages下载 GDAL 轮。

pip install "/path/to/GDAL?3.0.4?cp37?cp37m?win_amd64.whl"

Modify the libgdal.pyfile in the virtual envrironment site packages by adding 'gdal300' to line 23 of the Django GDAL package python file (/path/to/virtual_env/Lib/site-packages/django/contrib/gis/gdal/libgdal.py):

修改虚拟环境站点包中的libgdal.py文件,在Django GDAL包python文件(/path/to/virtual_env/Lib/site-packages/django/contrib/gis/gdal/libgdal)的第23行添加'gdal300' .py):

elif os.name == 'nt':
    # Windows NT shared libraries
    lib_names = ['gdal300', 'gdal204', 'gdal203', 'gdal202', 'gdal201', 'gdal20']

Finally, in your settings.pyfile in your Django project add

最后,在Django 项目的settings.py文件中添加

if os.name == 'nt':
    VENV_BASE = os.environ['VIRTUAL_ENV']
    os.environ['PATH'] = os.path.join(VENV_BASE, 'Lib\site-packages\osgeo') + ';' + os.environ['PATH']
    os.environ['PROJ_LIB'] = os.path.join(VENV_BASE, 'Lib\site-packages\osgeo\data\proj') + ';' + os.environ['PATH']