2

I could import gdal without any problem in my main Python script with:

from osgeo import gdal

I could also use gdalwarp, gdal_translate with no issue. But when it comes to gdal_calc, I got the error:

Traceback (most recent call last):
File "C:\Program Files (x86)\GDAL\gdal_calc.py", line 51, in <module>
    from osgeo import gdal
ImportError: No module named osgeo

I have tried to find out if anyone had the same issue but found nothing.

(In case you need more info: I installed gdal following the instructions from https://sandbox.idre.ucla.edu/sandbox/tutorials/installing-gdal-for-windows)

closed as off-topic by user30184, HDunn, mgri, whyzar, BERA Oct 29 '17 at 10:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions relating to general IT or with no clear GIS component, are off-topic here but can be researched/asked at Stack Overflow (software development), Super User (computing hardware and software), Database Administrators (relational databases) and other SE sites" – user30184, HDunn, mgri, whyzar, BERA
If this question can be reworded to fit the rules in the help center, please edit the question.

  • How did you launch gdal_calc.py? If you open the OSGeo4W shell and just write "gdal_calc" into the window it should run a batch file "gdal_calc.bat" that takes care of setting the environment for you. – user30184 Oct 28 '17 at 12:52
  • I called gdal_calc by using: os.system(command). With command is a string has the same format as: gdal_calc -A input1.tif -B input2.tif --outfile=result.tif --calc="A+B" – Kevin Oct 28 '17 at 12:58
  • Gdal_calc is itself a python script while gdalwarp and gdal_translate are executable programs. Probably that makes the difference. – user30184 Oct 28 '17 at 13:20
  • So any suggestions? – Kevin Oct 28 '17 at 13:32
  • I suggest to read python manuals or stackoverfow. For example stackoverflow.com/questions/1186789/… – user30184 Oct 28 '17 at 13:47
2

Comments are correct - gdalwarp, gdaltranslate etc. are running as EXEs sourced through your path variable - they are successfully importing the gdal and other modules, but via DLL files they reference (as opposed to the osgeo Python specific folder/module).

Depending on your installation, you should have either a GDAL or OSGEO folder that contains a sub-folder named "apps" - this is where these programs got placed on my install, anyway.

The "gdal_calc" command is calling the python script of the same name - line 51 in that is referenced in your error message (from osgeo import gdal).

To fix this, find out which "osgeo" folder has the Python modules (there are likely several folders with the name osgeo) - do this by finding the one that has the "init.py" file inside it. It should also have a "gdal.py" file, "ogr.py" file, and a "_gdal.pyd" file in there too. This osgeo folder is the most important, because it is the Python module from which you can import gdal, ogr, and osr.

Go into System->Environments... and either add the folder ABOVE the osgeo module to your Path variable, or else include the following command in your script, and it should run fine (note that the module here is where it appears on my system, yours will be slightly different):

import sys
sys.path.append(r'C:\Program Files (x86)\GDAL\gdal\python')

Then your os.system('gdal_calc...') command should run fine. But with the environment variable change, it'll be permanent and you won't have to manually repath to those Python modules every time you need to use them.

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