1

I know this has been asked somewhere else, but I can't find the solution for my environment:

  • macOS 12.6
  • Python 3.11.2
  • GDAL 3.6.3
  • Homebrew 4.0.10

Also I'm using Zsh.

I managed to install correctly GDAL (apparently) following this guide: https://mits003.github.io/studio_null/2021/07/install-gdal-on-macos/

Being on Monterey my Homerew cellars are under /opt/homebrew so I changed the instructions to:

  • python setup.py build_ext --gdal-config /opt/homebrew/Cellar/gdal/3.6.3/bin/gdal-config

I also had to add an entry to the zsh PATH to correctly locate Python scripts, because on macOS 12 this is different too (is in /Users/username/Library/Python and not /library/Python)

  • export PATH=$PATH:/Users/francescocretti/Library/Python/3.9/

At this point version 3.9 does not sound right, because my current Python version is 3.11 but even re-installing Python 3.11 this folder remained the same.

So I run the GDAL python scripts anyways and they worked. In fact, if I check GDAL version with gdalinfo --version I obtain GDAL 3.6.3, released 2023/03/07.

The problem is that if I try to import osgeo in a python shell I get this

Traceback (most recent call last):
  File "/Users/francescocretti/GDAL/GDAL-3.6.3/osgeo/__init__.py", line 30, in swig_import_helper
    return importlib.import_module(mname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'osgeo._gdal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/francescocretti/GDAL/GDAL-3.6.3/osgeo/__init__.py", line 46, in <module>
    _gdal = swig_import_helper()
            ^^^^^^^^^^^^^^^^^^^^
  File "/Users/francescocretti/GDAL/GDAL-3.6.3/osgeo/__init__.py", line 43, in swig_import_helper
    return importlib.import_module('_gdal')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '_gdal'

I suspect it does something to do with Python versions conflict but I could not solve the issue.

Any suggestions? :)

Thanks in advance

1 Answers1

0

I finally solved the problem. The issue is related to an incompatibility between your python and GDAL architecture CPU (see M1 Mac - GDAL Wrong Architecture Error [Django]).

In order to solve the issue, you need to specify the architecture during the installation of the python bindings (see (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))).

In our case, the first step is to uninstall the current version of python gdal :

 pip uninstall gdal

The second step is to reinstall gdal by specifying the CPU architecture (-arch arm64 in our case) :

ARCHFLAGS="-arch arm64" pip install gdal==$(gdal-config --version)  --compile --no-cache-dir

After that, you can check if everything is fine by executing the following command :

python3 -c "from osgeo import gdal"

Voila!