I'm having this issue when I try to use the Polygonise(raster to vector) tool in QGIS 3.4.3 (Madeira) on a Mac running Mojave:

File "/usr/local/opt/gdal2-python/bin/gdal_polygonize.py", line 36, in <module>

from osgeo import gdal

ImportError: No module named osgeo

Some others have discussed this before and with reference to this other thread "QGIS 3.0 error when start in MacOS High Sierra", I note that I don't seem to have the /Python/3.6/site-packages sub-branch in my


directories. I definitely have Python 3.6 (and not 3.7) installed.

To try to fix, I've reinstalled Python 3.6.8 using the installer from Python.org and I've reinstalled QGIS 3.4.3. Following these reinstalls, I still don't have the directory branches mentioned above and the Polygonise tool still doesn't work.

My presumption is that the lack of these branches is causing the problem (but I could be wrong). Can anyone kindly suggest what I need to do to sort this out?


Do you know for certain that Python 2 or Python 3 is being used?

Try python3, then issue the command import from osgeo import gdal.

roblabs@27:~ $ python3
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import from osgeo import gdal
>>> from osgeo import gdal
>>> # No response, nor errors => good news

That just worked for me on macOS. Your script that is calling the library gdal_polygonize.py is likely using macOS's installed version of Python 2, python.

You can also add #!/usr/local/bin/python3 to the top of your script.py to explicitly use python3

  • Thanks for the suggestion - unfortunately I get: Python 3.6.8 (v3.6.8:3c6b436a57, Dec 24 2018, 02:04:31) >>> import from osgeo import gdal File "<stdin>", line 1 import from osgeo import gdal ^ SyntaxError: invalid syntax
    – Stef_242
    Jan 21 '19 at 21:28
  • On the other hand, if I use only: >>> from osgeo import gdal then I get no error
    – Stef_242
    Jan 21 '19 at 21:33
  • Another idea — try these commands directly in the Python Console in QGIS. My test above was from the macOS bash shell.
    – RobLabs
    Jan 21 '19 at 22:41
  • Hm - I get the same result - the 'import' command gives an invalid syntax error and the 'from' command does not.
    – Stef_242
    Jan 23 '19 at 8:27

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.