Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named gdal

My GDAL is showing strange behaviour when I try to use ".py" functions from the command line:

1. For example, when I run gdalinfo --version, I get the standard response:

GDAL 1.11.3, released 2015/09/16

2. Also when I run gdalwarp, I get the standard response showing me the flags and everything.

3. Problem: However, when I run gdal functions with a .py extension, for example gdal_polygonize.py, I get the following:

Traceback <most recent call last>:
    File "C:\OSGeoW64\bin\gdal_polygonize.py", line 36, in <module>
    import gdal, ogr, osr
ImportError: No module named gdal

I checked my Path variable, reinstalled python and gdal, but nothing worked. When I run the gdal_polygonize.py function from my QGIS it works. I just can`t use it from my command line.

like image 200
maRtin Avatar asked Feb 19 '16 07:02

maRtin


People also ask

How do I know if Gdal is installed?

GDAL applications are run through the terminal. To test your installation, run the terminal command gdalinfo --version . A correct installation will output something like GDAL 1.9.


1 Answers

There are two parts to GDAL: the GDAL utilities (gdalinfo, gdalwarp, etc.) and the GDAL Python Bindings (when you call from osgeo import gdal from within a Python script.). The two can be (or could be in the past) installed separately.

The fact that you "see" gdalwarp on the command line means that you have the location of the utilities on your PATH (environment variables). Some of the utilities are self-contained, some require the Python bindings (notably those that require calling a .py file). In order for the Python bindings to work, GDAL has to be on the PYTHONPATH environment variable.

Find the GDAL folder (will contain gdalwarp, for instance). Find the osgeo folder in Python's Lib/site-packages. Add both of these locations to PATH and PYTHONPATH. You should now be able to from osgeo import gdal in a fresh Python shell.

If you cannot, either you are missing some files in those locations (bad install), or you have not specified the path correctly / set the right environment variables. There's no other magic involved, it's either installed and linked correctly, or you did something wrong.

Note that for environment variables to be set and working, you need to "Apply/OK" the environement variables window(s), and also start a fresh command prompt / Python shell.

like image 82
Benjamin Avatar answered Sep 28 '22 07:09

Benjamin