0

I am trying to use the OSGeo library and GDAL Python bindings in a script for Python 3.7.7 on MacOS 10.15.4 Catalina. However, when I try to compile the script, I receive the following error as soon as I import from the OSGeo module:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/osgeo/__init__.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 670, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 583, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 1043, in create_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ImportError: dlopen(/usr/local/lib/python3.7/site-packages/osgeo/_gdal.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/cfitsio/lib/libcfitsio.8.dylib
  Referenced from: /usr/local/opt/osgeo-gdal/lib/libgdal.27.dylib
  Reason: image not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import countries
  File "/Users/adamsamec/Dropbox/Work/Eurofly/eurofly-reverse-geocoder/countries.py", line 4, in <module>
    from osgeo import ogr
  File "/usr/local/lib/python3.7/site-packages/osgeo/__init__.py", line 32, in <module>
    _gdal = swig_import_helper()
  File "/usr/local/lib/python3.7/site-packages/osgeo/__init__.py", line 31, in swig_import_helper
    return importlib.import_module('_gdal')
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_gdal'

Here are the steps I've taken to install the osgeo and GDAL libraries, partially using the Homebrew package manager for Mac:

  1. Downloaded and installed this GDAL 2.4 Complete package.
  2. In Terminal, run the following commands:
pip3 install gdal
brew tap osgeo/osgeo4mac
brew install osgeo-gdal-python

Looks like the GDAL library is not properly installed because when I run gdalinfo, I get the following error:

dyld: Library not loaded: /usr/local/opt/cfitsio/lib/libcfitsio.8.dylib
  Referenced from: /usr/local/bin/gdalinfo
  Reason: image not found
zsh: abort      gdalinfo

Do you have any ideas what I am doing wrong?

2 Answers 2

1

If you use the GDAL KingChaos version 2.4, you have not read the ReadMe.rtf file.

Python components now require Python 3.6 from python.org, make sure that the default install is used, or at least installing pip in a custom install. If Python 3.6 is not found the Python components will not be installed, and you will have to run this installer again after installing Python 3.6. Numpy and GDAL-python will be installed with pip (from a local installer copy, but not upgraded).

And you use a Python 3.7.7 version (Installed how, from python.org, Homebrew, Anaconda ?)

Otherwise the libraries are installed in /Library/Frameworks/GDAL.framework and the a local installer copy of GDAL-python is /Library/Frameworks/GDAL.framework/Versions/2.4/Resources/GDAL-2.4.2-cp36-cp36m-macosx_10_9_x86_64.whl

7
  • Thanks, @gene. I have installed Python via Homebrew, so how should I proceed to make it work if I want to keep Python 3.7.7?
    – Adam
    Commented May 23, 2020 at 20:46
  • Install GDAL from Homebrew
    – gene
    Commented May 24, 2020 at 8:52
  • That's what I did, but the same errors persist..
    – Adam
    Commented May 24, 2020 at 9:06
  • It is a Homebrew problem and not a GDAL problem ((python) Seeming incompatibility between libgdal and libcfitsio on MacOS X for example) -> open an issue in github.com/OSGeo/gdal/issues and delete the GDAL KingChaos version
    – gene
    Commented May 24, 2020 at 13:39
  • 1
    @Adam the only thing to do is brew upgrade osgeo-gdal osgeo-gdal-python. that's all. :-)
    – lrssvt
    Commented May 26, 2020 at 11:31
0

As @lrssvt has commented, the osgeo-gdal and osgeogdal-python formulas for Homebrew have been updated. Upgrading these solves the problem. Therefore the answer is to run the following in the Terminal:

brew upgrade osgeo-gdal
brew upgrade osgeo-gdal-python

Since this answer does not require downgrading Python to the version 3.6.x, and makes osgeo and GDAL libraries work in the Python version available currently in Homebrew, , that is, Python 3.7.7, I am marking this answer as accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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