Django & GDAL - Could not find the GDAL library
Asked Answered
R

2

5

I have a macbook with the M1 Chip and I'm trying to set up a django project. The project works with Geospatial libraries gdal. I installed gdal with homebrew on version 3.3.1_3 and inside my virtual env version 3.3.1

If I type python manage.py migrate I get an error:

django.core.exceptions.ImproperlyConfigured: Cloud not find the GDAL library (tried "gdal", "GDAL", "gdal3.1.0", "gdal3.0.0", "gdal2.4.0", "gdal2.3.0", "gdal2.2.0", "gdal2.1.0"). Is GDAL installed? If it is, try settings GDAL_LIBRARY_PATH in your settings.

By pip list shows me GDAL at version 3.1.0. The Django Docs say version 3.3.x is not supported but I cant install a specific gdal version with homebrew.

Is there a different solution to get the django project up and running?

Robert answered 2/9, 2021 at 10:13 Comment(1)
Suggest setting up your virtual env using miniconda. You will have more control over the versions, and the gdal binaries will get installed in there.Serranid
G
9

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'
-->

Ginnie answered 8/1, 2023 at 14:17 Comment(2)
Dude, you are my hero! This works. Thank you so much.Dustheap
respect++, mad mad respectInsulation
R
5

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.

Reluct answered 31/5, 2022 at 5:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.