I am getting an error when I try to import osgeo from the python console on my mac (OS 10.13.6):

>>> import osgeo
ModuleNotFoundError: No module named 'osgeo'

As far as I know, GDAL is installed properly because I can get it's version from the bash command line:

$ gdalinfo --version
GDAL 2.2.4, released 2018/03/19

In case my python version is relevant:

$ python --version
Python 3.7.0

I installed it when I installed QGIS and interestingly import osgeo is working just fine from the QGIS python console. Any tips how to get it to work in the plain interactive console in Terminal?

  • If is QGIS 2.18.x, you must use Python 2.7 and in the case of 3.x, Python 3.6. So, you are using another Python installed in your machine. – aldo_tapia Aug 8 '18 at 22:15
  • GDAL is indeed installed but have you installed the python libs for osgeo? Is there a folder in your python install site-packages named osgeo? Do you have more than one python install? – Michael Stimson Aug 9 '18 at 3:24
  • 1
    @aldo_tapia I am using QGIS 3, but I think the problem might indeed have to do with having multiple versions installed. – lucas Aug 9 '18 at 11:57

If you have installed GDAL with Homebrew:

  • from osgeo4mac:
    • Python 2.x / QGIS 2.x: /usr/local/Cellar/gdal2-python/2.X.X/lib/python2.7/site-packages/
    • Python 3.x / QGIS 3.x: /usr/local/Cellar/gdal2-python/2.X.X/lib/python3.6/site-packages/
  • from homebrew core:
    • Python 2.x / QGIS 2.x: /usr/local/Cellar/gdal/2.x.x/lib/python2.7/site-packages
    • Python 3.x / QGIS 3.x: /usr/local/Cellar/gdal/2.x.x/lib/python3.6/site-packages

Before running Python, just do

EXPORT PYTHONPATH=_one_of_above_

  • Thanks Denis, I couldn't remember how I installed GDAL but it appears that it wasn't with Homebrew (based on the directory where I found it). Adding to the python path was the correct solution though! – lucas Aug 9 '18 at 11:59

The following solved it for me.

I found the location of the GDAL site-packages by typing osgeo in the python console in QGIS:

>>> osgeo
<module 'osgeo' from '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/osgeo/__init__.py'>

I read that the directory where your module lives has to be in your python path, and when I checked it wasn't there:

>>> import sys
>>> sys.path
['', '/Library/Frameworks/GDAL.framework/Versions/2.1/Python/2.7/site-packages', '/Library/Frameworks/GDAL.framework/Versions/2.1/Python/2.7/site-packages/osgeo', '/Users/<my_user_name>', '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages', '/usr/local/Cellar/numpy/1.15.0/libexec/nose/lib/python3.7/site-packages']

So I added that path to my python path. For me that meant editing my ZSH profile (vi ~/.zshrc), but if you don't use ZSH you'll want to edit your bash profile or whatever you do use. In any case, I just added this line:

export PYTHONPATH="/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages:$PYTHONPATH"` to my bash profile

Then I reloaded my profile (source ~/.zshrc or open a new shell), started the python console again, and it worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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