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

后端 未结 8 1503
青春惊慌失措
青春惊慌失措 2020-11-28 06:58

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

相关标签:
8条回答
  • 2020-11-28 07:36

    I have found the following to work for windows:

    • Run python to check if your python is 32 or 64 bit.
    • Install corresponding OSGeo4W (32 or 64 bit) into C:\OSGeo4W or 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 check to verify geodjango is working correctly.

    0 讨论(0)
  • 2020-11-28 07:43

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

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

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

    No negative consequences for now.

    0 讨论(0)
  • 2020-11-28 07:43
    1. Download GDAL wheel file that is supported for your platform from here.
    2. Open the command window where the downloaded file is located and activate your virtual environment.

    Activating your virtual environment

    1. Then install the wheel using command pip install name_of_the_file .

    Screenshot 2

    You will see osgeo folder has been created in the location '...\Envs\my_django\Lib\site-packages\' .

    1. Go to osgeo folder and copy the entire path of your gdalxxx.dll file and add to setting.py file as gdal library path. For example

      GDAL_LIBRARY_PATH = r'C:\Users\WIN8\Envs\my_django\Lib\site-packages\osgeo\gdal300.dll'

    0 讨论(0)
  • 2020-11-28 07:45

    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:\OSGeo4W or 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:
    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.
    set OSGEO4W_ROOT=C:\OSGeo4W
    set PYTHON_ROOT=C:\Python3X 
    set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal 
    set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
    
    0 讨论(0)
  • 2020-11-28 07:54

    I had the same error "The specified module could not be found," even though gdal204.dll was present at the expected location, with the right architecture (which I verified by adding asserts in the Python code and loading the DLL from a C program).

    It turned out to be an issue with the Python 3.7 app from the Microsoft Store, the one that automatically installs the first time you type python on the command line. This version of Python refuses to load the GDAL DLL; I don't know if it's a bug or a security feature.

    I fixed the issue by:

    • uninstalling the Python app
    • removing the app execution aliases (as explained in the app description)
    • installing the regular Python package from python.org
    0 讨论(0)
  • 2020-11-28 07:55

    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 gdal203 in the lib_names list 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):

    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.

    0 讨论(0)
提交回复
热议问题