[Django]-Django & GDAL – Could not find the GDAL library

7👍

Unfortunately the suggestions by @Ameya didn’t work for me, but they did send me down the right path in my investigation. The root cause of my issue was related to python ctypes, from the pyenv distro that I use, not being able to find the GDAL shared library installed from homebrew. Below are some other explanations that I found during my investigation.

The following thread on python bugs gives some good background and detail into Apple’s semi-recent(?) increased security constraints that were causing the underlying issue:

https://bugs.python.org/issue43964

This post on pyenv’s github gives excellent detail on the issue and why pyenv does not have a general purpose solution:

https://github.com/pyenv/pyenv/issues/2339

Here is one way to test if you are having the same underlying issue that I did. Start a python shell and try to manually load the library with ctypes:

╰─➤ python3
Python 3.10.8 (main, Dec 5 2022, 14:59:49) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
–> from ctypes.util import find_library
–> find_library(‘libgdal.dylib’)

If ctypes finds the library, a path is shown. If no lib is found, no response is given.

It appears that the ctypes loader now(?) only has a fixed set of paths on macos. The work-around that I used is to simply find one of the existing paths that was not used. I then created a symbolic link there, pointing to the homebrew libs location. For example ctypes appears to search $HOME/lib and /usr/local/lib, both of which were empty (or non-existent) on my machine. So I created a symbolic link as follows:

╰─➤ sudo ln -s /opt/homebrew/lib /usr/local/lib

or

╰─➤ ln -s /opt/homebrew/lib ~/lib

Now ctypes finds the library and as a result, django starts properly:

╰─➤ python3
Python 3.10.8 (main, Dec 5 2022, 14:59:49) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
–> from ctypes.util import find_library
–> find_library(‘libgdal.dylib’)
‘/usr/local/lib/libgdal.dylib’
–>

👤jvp

3👍

I happen to own new MacBook M1 Pro and facing some issue. Here are the steps I did that helped me resolved this issue.

Step 1: First check whether you have installed gdal and geos.

gdal-config --version

If no version shows up, then run brew install gdal in your terminal.

Step 2: Find out path of your gdal installation.

which gdal-config

This should return in case of M1 Mac OS –

/opt/homebrew/opt/gdal/bin

Step 3: Check if gdal lib file is present in below location.

/opt/homebrew/opt/gdal/lib/libgdal.dylib

If it does, then great! Do the same setps for geos if you require that as well.

Once all done, in your Django Project in Settings.py,
Add below lines.

GDAL_LIBRARY_PATH = '/opt/homebrew/opt/gdal/lib/libgdal.dylib'

GEOS_LIBRARY_PATH = '/opt/homebrew/opt/geos/lib/libgeos_c.dylib'

It should work now. Also if you have installed and using PostgreSQL it does come with GDAL and GEOS libs, you can also give those paths as well if needed.

👤Ameya

Leave a comment