MacOS Ventura下Django无法运行:GDAL加载libtiff版本错误
问题描述
在MacOS Ventura系统上运行Django时遇到GDAL无法加载libtiff库的问题。
已确认系统中安装了libtiff:
% otool -L /opt/homebrew/lib/libgdal.dylib | grep libtiff /opt/homebrew/opt/libtiff/lib/libtiff.6.dylib (compatibility version 7.0.0, current version 7.1.0)
但错误信息显示,GDAL尝试加载libtiff.5.dylib,而当前安装的是libtiff.6.dylib。运行python manage.py runserver时的错误日志如下:
% python manage.py runserver Watching for file changes with StatReloader 2023-10-01 17:14:00,801 INFO Watching for file changes with StatReloader Exception in thread django-main-thread: Traceback (most recent call last): File "/opt/homebrew/Cellar/python@3.9/3.9.18/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 980, in _bootstrap_inner self.run() File "/opt/homebrew/Cellar/python@3.9/3.9.18/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 917, in run self._target(*self._args, **self._kwargs) File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/utils/autoreload.py", line 54, in wrapper fn(*args, **kwargs) File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run autoreload.raise_last_exception() File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception raise _exception[1] File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/core/management/__init__.py", line 337, in execute autoreload.check_errors(django.setup)() File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/utils/autoreload.py", line 54, in wrapper fn(*args, **kwargs) File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate app_config = AppConfig.create(entry) File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/apps/config.py", line 90, in create module = import_module(entry) File "/opt/homebrew/Cellar/python@3.9/3.9.18/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1030, in _gcd_import File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 850, in exec_module File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/mapwidgets/__init__.py", line 4, in <module> from .widgets import GooglePointFieldWidget, GooglePointFieldInlineWidget, \ File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/mapwidgets/widgets.py", line 5, in <module> from django.contrib.gis.forms import BaseGeometryWidget File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/contrib/gis/forms/__init__.py", line 3, in <module> from .fields import ( # NOQA File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/contrib/gis/forms/fields.py", line 2, in <module> from django.contrib.gis.gdal import GDALException File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/contrib/gis/gdal/__init__.py", line 28, in <module> from django.contrib.gis.gdal.datasource import DataSource File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/contrib/gis/gdal/datasource.py", line 39, in <module> from django.contrib.gis.gdal.driver import Driver File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/contrib/gis/gdal/driver.py", line 5, in <module> from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/contrib/gis/gdal/prototypes/ds.py", line 9, in <module> from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal File "/Users/myproj/Documents/project/projectdotcom/env_m1/lib/python3.9/site-packages/django/contrib/gis/gdal/libgdal.py", line 47, in <module> lgdal = CDLL(lib_path) File "/opt/homebrew/Cellar/python@3.9/3.9.18/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ctypes/__init__.py", line 374, in __init__ self._handle = _dlopen(self._name, mode) OSError: dlopen(/opt/homebrew/lib/libgdal.dylib, 0x0006): Library not loaded: /opt/homebrew/opt/libtiff/lib/libtiff.5.dylib Referenced from: <3FEB9C56-E432-36C6-A940-FC254CC92FAB> /opt/homebrew/Cellar/libgeotiff/1.7.1_1/lib/libgeotiff.5.dylib Reason: tried: '/opt/homebrew/opt/libtiff/lib/libtiff.5.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/opt/libtiff/lib/libtiff.5.dylib' (no such file), '/opt/homebrew/opt/libtiff/lib/libtiff.5.dylib' (no such file), '/usr/local/lib/libtiff.5.dylib' (no such file), '/usr/lib/libtiff.5.dylib' (no such file, not in dyld cache), '/opt/homebrew/Cellar/libtiff/4.6.0/lib/libtiff.5.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/opt/homebrew/Cellar/libtiff/4.6.0/lib/libtiff.5.dylib' (no such file), '/opt/homebrew/Cellar/libtiff/4.6.0/lib/libtiff.5.dylib' (no such file), '/usr/local/lib/libtiff.5.dylib' (no such file), '/usr/lib/libtiff.5.dylib' (no such file, not in dyld cache)
解决方案
从错误日志可知,并非GDAL直接依赖libtiff.5,而是libgeotiff依赖旧版本的libtiff。以下是几种可行的解决方法:
方法一:创建软链接兼容旧版本
直接在libtiff安装目录下创建指向新版本的软链接,让系统能找到libtiff.5.dylib:
sudo ln -s /opt/homebrew/opt/libtiff/lib/libtiff.6.dylib /opt/homebrew/opt/libtiff/lib/libtiff.5.dylib
创建完成后,重新运行python manage.py runserver测试是否正常。
方法二:重新编译libgeotiff适配新版本libtiff
如果软链接方法出现兼容性问题,可重新编译libgeotiff,使其链接到当前的libtiff.6:
brew reinstall libgeotiff --build-from-source
等待编译完成后,再次启动Django服务。
方法三:安装指定版本的libtiff
若上述方法均无效,可安装libtiff的5.x版本以匹配依赖:
brew install libtiff@5
安装完成后,需确保系统优先使用该版本,可通过设置环境变量:
export PATH="/opt/homebrew/opt/libtiff@5/bin:$PATH" export LDFLAGS="-L/opt/homebrew/opt/libtiff@5/lib" export CPPFLAGS="-I/opt/homebrew/opt/libtiff@5/include"
若需永久生效,可将这些环境变量添加到~/.zshrc或~/.bash_profile文件中。
内容的提问来源于stack exchange,提问作者Axil
相关产品推荐
相关产品推荐

