1. 参考
https://docs.djangoproject.com/en/5.0/ref/contrib/gis/install/#macos
2. 问题描述
苹果系统 Mac 中搭建 GeoDjango 开发环境时出现以下报错:
File "*.venv/lib/python3.8/site-packages/django/contrib/gis/gdal/libgdal.py", line 60, in <module>
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Could not find the GDAL library (tried "gdal", "GDAL", "gdal3.4.0", "gdal3.3.0", "gdal3.2.0", "gdal3.1.0", "gdal3.0.0", "gdal2.4.0", "gdal2.3.0", "gdal2.2.0"). Is GDAL installed? If it is, try setting GDAL_LIBRARY_PATH in your settings.
3. 解决步骤
3.1. 安装依赖包
sudo port install postgresql13-server
sudo port install geos
sudo port install proj6
sudo port install postgis3
sudo port install gdal
sudo port install libgeoip
pip install GDAL=="$(gdal-config --version).*"
3.2. 配置 GDAL_LIBRARY_PATH
sudo find / -name "libgdal*"
找到 libgdal.dylib 的路径
/opt/local/lib/libgdal.dylib
settings.py 中添加以下代码:
GDAL_LIBRARY_PATH = '/opt/local/lib/libgdal.dylib'
3.3. 配置 GEOS_LIBRARY_PATH
sudo find / -name "libgeos_c*"
找到 libgeos_c.dylib 的路径
/opt/local/lib/libgeos_c.dylib
settings.py 中添加以下代码:
GEOS_LIBRARY_PATH = '/opt/local/lib/libgeos_c.dylib'