Red Hat EL7.3集群中Python3.5导入gdal_array失败问题求助
gdal_array Import Error with GDAL 2.2.4 on Red Hat EL 7.3 Hey there, I’ve run into this exact issue before with older GDAL versions on RHEL systems. Let’s break down what’s going on and how to fix it.
Your Setup & Error Details
You’re running Red Hat EL 7.3 with GDAL 2.2.4 and Python 3.5. You can successfully import gdal, osgeo, and osr, but importing gdal_array throws an error. Your code and truncated traceback look like this:
import gdal import osgeo import osr from osgeo import gdal_array
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/.local/lib/python3.5/site-packages/osgeo/gdal_array.py", line 28, in <module> _gdal_array = swig_import_helper() File "/.local/lib/python3.5/site-packages/osgeo/gdal_array.py", line 24, in swig_import_helper _mod = imp.load_module('_gdal_array', fp, pathname, description) File "/usr/lib64/python3.5/imp.py", line 242, in load_module return load_dynamic(name, filename, file) File "/usr/lib64/python3.5/imp.py", line 342, in load_dynamic return _load(spec) ImportError: libgdal.so.20: cannot open shared object file: No such file or directory
(Note: The full error almost always points to a missing shared library or mismatched Python bindings)
Solutions to Try
Fix the Shared Library Path
The most common culprit is that your system can’t locate the GDAL shared library. First, find wherelibgdal.so.20lives:find / -name libgdal.so.20 2>/dev/nullOnce you get the path (e.g.,
/usr/local/lib), add it to your library path temporarily:export LD_LIBRARY_PATH=/path/to/gdal/lib:$LD_LIBRARY_PATHTo make this change permanent, add that line to your
~/.bashrcor~/.bash_profileand log back in to apply it.Recompile GDAL with Python & Array Bindings
If you installed GDAL from source, you might have skipped enabling the Python array bindings. First, install required dependencies:yum install python35-devel numpyThen recompile GDAL with the correct flags:
./configure --with-python --enable-shared make clean && make && make installAfter compiling, navigate to the
swig/pythondirectory in the GDAL source and install the Python bindings properly:cd swig/python python3 setup.py installAlign Python GDAL Package with System GDAL
Sometimes the GDAL Python package installed via pip doesn’t match your system’s GDAL version. Uninstall the existing package first:pip3 uninstall gdalThen install the exact version matching your system’s GDAL:
pip3 install gdal==2.2.4Try Alternative Import Syntax
Older GDAL versions sometimes use different import paths forgdal_array. Give these alternatives a shot:import gdal_arrayOr:
from osgeo.gdal_array import *
内容的提问来源于stack exchange,提问作者Chris G

