【问题标题】:OSError in Geodjango: [WinError 127] : The specified procedure could not be foundGeodjango 中的 OSError:[WinError 127]:找不到指定的程序
【发布时间】:2018-08-14 03:58:11
【问题描述】:

我尝试将DjangoGeodjango 一起使用,我遵循了所有文档步骤,但在尝试进行迁移时仍然出现此错误:

OSError: [WinError 127] : The specified procedure could not be found

models.py 是正确的,我使用的是来自geodjango 站点,这是我的settings.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ci3qd6ti+d+#-4pae7d*)^8nmmgb2j@cacivgt8rdvsngu2$mj'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

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

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'geodjango.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'geodjango.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'test',
    'USER': 'postgres',
    'PASSWORD': '*********',
    'HOST': '127.0.0.1',
    'PORT': '5432',
    }

}


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'

这是我得到的错误:

>>> python manage.py makemigrations

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\core\management\__init__.py", line 357, in execute
    django.setup()
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Users\hades\AppData\Local\Programs\Python\Python37\Lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\hades\PycharmProjects\meumapa\geodjango\world\models.py", line 3, in <module>
    from django.contrib.gis.db import models
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\contrib\gis\db\models\__init__.py", line 3, in <module>
    import django.contrib.gis.db.models.functions  # NOQA
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\contrib\gis\db\models\functions.py", line 4, in <module>
    from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\contrib\gis\db\models\fields.py", line 3, in <module>
    from django.contrib.gis import forms, gdal
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\contrib\gis\forms\__init__.py", line 3, in <module>
    from .fields import (  # NOQA
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\contrib\gis\forms\fields.py", line 2, in <module>
    from django.contrib.gis.geos import GEOSException, GEOSGeometry
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\contrib\gis\geos\__init__.py", line 5, in <module>
    from .collections import (  # NOQA
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\contrib\gis\geos\collections.py", line 9, in <module>
    from django.contrib.gis.geos.geometry import GEOSGeometry, LinearGeometryMixin
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\contrib\gis\geos\geometry.py", line 8, in <module>
    from django.contrib.gis import gdal
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\contrib\gis\gdal\__init__.py", line 28, in <module>
    from django.contrib.gis.gdal.datasource import DataSource
  File "C:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\contrib\gis\gdal\datasource.py", line 39, in <module>
    from django.contrib.gis.gdal.driver import Driver
  File "C:\Users\hades\PycharmProjects\geodjango\venv\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:\Users\hades\PycharmProjects\geodjango\venv\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:\Users\hades\PycharmProjects\geodjango\venv\lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 47, in <module>
    lgdal = CDLL(lib_path)
  File "C:\Users\hades\AppData\Local\Programs\Python\Python37\Lib\ctypes\__init__.py", line 356, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 127] The specified procedure could not be found

我检查并安装了geodjango 的所有要求。

有什么建议吗?

【问题讨论】:

  • 有没有找到解决这个问题的方法?
  • 2021 年仍在寻找解决方案

标签: python django python-3.x geodjango


【解决方案1】:

您可能有多个 Python 版本并且它们之间存在冲突。

尝试在您的 virtualenv 上安装 GDAL,并确保 gdal202.dll 在您的 virtualenv 上下文中。

还要确保您使用 pip 并在您的 virtualenv 中从 https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal 安装了正确的编译版本。

GDAL DLL 将位于 gdal 安装路径中。我在 settings.py 中使用这样的代码在 Windows 上设置 GeoDjango:

try:
    import gdal
    gdal_path = Path(gdal.__file__)
    OSGEO4W = os.path.join(gdal_path.parent, 'osgeo')
    os.environ["OSGEO4W_ROOT"] = OSGEO4W
    os.environ["GDAL_DATA"] = os.path.join(OSGEO4W, "data", "gdal")
    os.environ["PROJ_LIB"] = os.path.join(OSGEO4W, "data", "proj")
    os.environ["PATH"] = OSGEO4W + ";" + os.environ["PATH"]
    GEOS_LIBRARY_PATH = str(os.path.join(OSGEO4W, "geos_c.dll"))
    GDAL_LIBRARY_PATH = str(os.path.join(OSGEO4W, "gdal301.dll"))
except ImportError:
    GEOS_LIBRARY_PATH = None
    GDAL_LIBRARY_PATH = None

【讨论】:

  • “确保 gdal202.dll 在你的 venv 中”是什么意思?就我而言,我使用 OSgeo4W 将其安装在我的系统上(v3.0.4)。我自己从源代码构建了 gdal(Python)(Wheels 不适用于 Python 3.7 和 GDAL 3.0.4,我无法使用 OSgeo4W 安装 3.1.4)。不幸的是,我不断收到此错误。
  • 在已编译的 gdal 上,您将拥有 dll 文件。我编辑了答案,显示了您必须设置的环境变量才能让 GeoDjango 在 Windows 上运行。使用您编译版本的正确 gdal dll 更改 gdal301.dll。
【解决方案2】:

“Geodjango 中的 OSError: [WinError 127] : The specified procedure could not be found” 此错误是由于 D-Jango 和 G-dal 库的不匹配版本引起的。请访问 Geo-Django 的官方文档以获取支持的 G-Dal 版本的详细信息。一旦匹配并安装了正确的版本,一切都会顺利进行。

注意:有些论坛说您可以导航到此路径 "Lib\site-packages\django\contrib\gis\gdal\libgdal.py" 并打开 libgdal.py Python 文件,然后将 DLL 名称(例如 gdal303.dll)添加到 lib_names 列表中。 这不是必需的

如果您使用的是正确版本,则肯定会安装所需的库并出现在相应的 python 文件中。

这是您可以用来检查版本的命令

所有已安装包的详细信息:pip freeze

DJnago 版本:django-admin --version

Gdal 版本:gdalinfo --version

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2020-10-22
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多