[GIS] No module named ‘osgeo’ after installing GDAL on Mac

gdalosgeopython

I installed GDAL on Mac and it was successful because when I type in the terminal : gdalinfo --version it renders me the right version. The issue is when I switch to Pycharm or the default IDLE of Python and I run this simple program:

import sys
from osgeo import gdal

version_num = int(gdal.VersionInfo('VERSION_NUM'))
if version_num < 1100000:
    sys.exit('ERROR: Python bindings of GDAL 1.10 or later required')

It returns me this error :

Traceback (most recent call last):
  File "/Users/sigc2sige/Desktop/Zak/Gdal/app1.py", line 2, in <module>
    from osgeo import gdal
ModuleNotFoundError: No module named 'osgeo'

Process finished with exit code 1

Any ideas please to solve this issue?

Best Answer

In addition to installing GDAL, you also need to install the GDAL Python bindings. You do this with the command

pip install gdal

If you are using python3 on a Linux or MacOS system, use this command.

pip3 install gdal

(On Windows, it would be best to specify the full path the correct pip executable, as they are not named for the Python version like they are for the other systems)

I don't know much about your MacOS environment and how you installed Python, so if this does not work, you would need to adjust your environment settings first to make pip work properly.

I am also not sure why you are trying to import gdal as part of OSGEO. Did you install GDAL by itself, or did you install OSGEO? You say you installed GDAL, no mention of OSGEO. So you should probably just try

import gdal

from inside Python, instead of from osgeo import gdal.

You will also want to double-check that Python can find your GDAL installation. We already know the folder is in your system path variable, since you are able to execute gdalinfo. Type the command which gdalinfo and note the path to that program (without the gdalinfo name at the end). You need to add this path either to the PYTHONPATH environment variable (I think this would only work when Python is started from your command shell), or add a .pth file to your Python Lib/site-packages folder (see this help page)