0

I have the recent version of QGIs/GDAL installed.

If I try to execute gdal_calc.py, I get the following error message:

`Traceback (most recent call last): File "C:\OSGeo4W64\apps\Python37\lib\site-packages\osgeo_init_.py", line 18, in swig_import_helper return importlib.import_module(mname) File "C:\OSGeo4W64\apps\Python37\lib\importlib_init_.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1006, in _gcd_import File "", line 983, in _find_and_load File "", line 967, in _find_and_load_unlocked File "", line 670, in _load_unlocked File "", line 583, in module_from_spec File "", line 1043, in create_module File "", line 219, in _call_with_frames_removed ImportError: DLL load failed: Das angegebene Modul wurde nicht gefunden.

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Program Files\GDAL\gdal_calc.py", line 5, in from osgeo.utils.gdal_calc import * # noqa File "C:\OSGeo4W64\apps\Python37\lib\site-packages\osgeo_init_.py", line 32, in gdal = swig_import_helper() File "C:\OSGeo4W64\apps\Python37\lib\site-packages\osgeo_init.py", line 31, in swig_import_helper return importlib.import_module('gdal') File "C:\OSGeo4W64\apps\Python37\lib\importlib_init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) ImportError: DLL load failed: Das angegebene Modul wurde nicht gefunden.

E:\UniversitaetHD\WS 2020 - 2021\Seminar GIS\Gruppenarbeit\Dateien>PAUSE`

Seems like those problems to me:

ImportError: DLL load failed with error code when I try to import GDAL from the Python console gdal ImportError in python on Windows

Unfortunately, none of these solutions work for me.

My system variables are already right configured:

enter image description here

For the path system variable

While executing from osgeo import gdal in python, the same error message appears.

I installed the latest GDAL versions from here: https://www.gisinternals.com/release.php

enter image description here

enter image description here

9
  • 1
    Your gdal_data does not look right. It should be just one directory.
    – user30184
    Commented Mar 3, 2021 at 19:21
  • In several other issues where users have the same problem, they suggested adding both of them. I also tried it with only one directory, didnt work either. Here it is suggested: stackoverflow.com/questions/35500176/… (in the first question with 8 upvotes)
    – Nokturius
    Commented Mar 3, 2021 at 19:23
  • One is enough. Do you have now osgeo module installed in path c:\OSGeo4W64\apps\Python37\Lib\site-packages\osgeo? Then doing set path=%path%;c:\OSGeo4W64\apps\Python37 could be enough. EDIT: you seem to have python37 included in PATH already.
    – user30184
    Commented Mar 3, 2021 at 19:38
  • I actually have osgeo in this path... Then I should do your recommend command? EDIT: Yeah, python37 is already in my PATH.
    – Nokturius
    Commented Mar 3, 2021 at 19:40
  • There is definitely something wrong with your Python installation or configuration. But if you have OSGeo4W why don't you use Python tools from OSGeo4W shell? Ah, but perhaps you have now two osgeo modules, one from OSGeo4W and another installed with the installer, in some place. I recommend to install Python37 into a dedicated directory and use the msi installer against that if you want to use both OSGeo4W and gisinternals installations. That's how I have them installed.
    – user30184
    Commented Mar 3, 2021 at 19:47

1 Answer 1

2

This works for me:

  1. Download and install Python 3.7. I don't like "Program Files" much but in this test I used C:\Program Files\Python37.
  2. Install GDAL with the MSI installer. I used C:\Program Files\GDAL.
  3. Install GDAL Python bindings with the MSI installer. Use the Python37 location from point 1) and check that you have osgeo package in C:\Program Files\Python37\Lib\site-packages\ after installation.

GDAL installer creates a shortcut into Start menu. Open the GDAL command prompt from there. The gdal_data should look like this

C:\Program Files\GDAL>set gdal_data
GDAL_DATA=C:\Program Files\GDAL\gdal-data

At this moment GDAL does not know where to find Python. For the session that was just opened it can be set with

C:\Program Files\GDAL>set path=%path%;"c:\program files\Python37"

For making the setting permanent edit PATH in this file

C:\Program Files\GDAL>more gdalshell.bat
@echo off
@echo Setting environment for using the GDAL Utilities.
set SDK_ROOT=%~dp0
set SDK_ROOT=%SDK_ROOT:\\=\%
SET "PATH=%SDK_ROOT%;%PATH%"
SET "GDAL_DATA=%SDK_ROOT%gdal-data"
SET "GDAL_DRIVER_PATH=%SDK_ROOT%gdalplugins"
SET "PROJ_LIB=%SDK_ROOT%projlib"
SET "PYTHONPATH=%SDK_ROOT%python"

When the Python path is set especially for the GDAL shell it does not have any effect on other applications that use Python.

Now it is possible to do couple of tests.

C:\Program Files\GDAL>python
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from osgeo import gdal
>>>

This looks good.

C:\Program Files\GDAL>python gdal_calc.py
Traceback (most recent call last):
  File "gdal_calc.py", line 5, in <module>
    from osgeo.utils.gdal_calc import *  # noqa
  File "c:\program files\Python37\lib\site-packages\osgeo\utils\gdal_calc.py", line 58, in <module>
    import numpy
ModuleNotFoundError: No module named 'numpy'

Ok, numpy is missing so add it_:

C:\Program Files\GDAL>python -m pip install numpy
Defaulting to user installation because normal site-packages is not writeable
Collecting numpy
  Downloading numpy-1.20.1-cp37-cp37m-win_amd64.whl (13.6 MB)
     |████████████████████████████████| 13.6 MB 3.3 MB/s
Installing collected packages: numpy

Notice: Because of special handling that Windows has for "Program Files" numpy may get installed into users settings. Therefore in might be better to install Python in step 1) into dedicated directory like c:\python37.

Now try gdal_calc again:

C:\Program Files\GDAL>gdal_calc.py
Usage: gdal_calc.py --calc=expression --outfile=out_filename [-A filename]
                    [--A_band=n] [-B...-Z filename] [other_options]
1
  • Thank you very much! It finally worked for me! :)
    – Nokturius
    Commented Mar 4, 2021 at 0:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.