在AWS AMI 2中编译带Python绑定的libgdal失败求助
问题:AWS Lambda Python 3.9镜像中编译安装GDAL Python绑定失败
我需要在基于Amazon Linux 2的AWS Lambda Python 3.9镜像(public.ecr.aws/lambda/python:3.9)中编译安装带Python绑定的libgdal,用于ECR镜像构建。当前使用的Dockerfile如下:
# Pull the base image with python 3.9 as a runtime for your Lambda FROM public.ecr.aws/lambda/python:3.9 # install tools (gcc to compile C code during installation, mesa-libGL for OpenCV) RUN yum install -y yum-plugin-remove-with-leaves tar file.x86_64 sudo make gcc git unzip mesa-libGL gcc-c++.x86_64 cpp.x86_64 sqlite-devel.x86_64 libtiff.x86_64 cmake3.x86_64 RUN python3.9 -m pip install numpy==1.22.4 WORKDIR /tmp RUN curl https://download.osgeo.org/proj/proj-6.1.1.tar.gz -o proj-6.1.1.tar.gz && \ tar -xvf proj-6.1.1.tar.gz && \ cd proj-6.1.1 && \ ls -l && \ ./configure --disable-dependency-tracking && \ make && \ sudo make install && \ cd .. && \ rm -rf proj-6.1.1 && \ rm -f proj-6.1.1.tar.gz RUN yum install -y which RUN ls -lr `which python` RUN git clone --branch patch/3.2.2.1 https://github.com/OSGeo/gdal.git && \ cd gdal/gdal && \ ./configure --with-proj=/usr/local --with-python && \ make && \ sudo make install && \ cd ../.. && \ rm -rf gdal RUN which gdalinfo && \ gdalinfo --version && \ ls -l /usr/include/gdal
执行make install时出现以下错误:
(cd port; make install) make[1]: Entering directory `/tmp/gdal/gdal/port' for f in cpl_atomic_ops.h cpl_config_extras.h cpl_config.h cpl_conv.h cpl_csv.h cpl_error.h cpl_hash_set.h cpl_http.h cpl_json.h cplkeywordparser.h cpl_list.h cpl_minixml.h cpl_minizip_ioapi.h cpl_minizip_unzip.h cpl_minizip_zip.h cpl_multiproc.h cpl_odbc.h cpl_port.h cpl_progress.h cpl_quad_tree.h cpl_spawn.h cpl_string.h cpl_time.h cpl_virtualmem.h cpl_vsi.h cpl_vsi_error.h cpl_vsi_virtual.h gdal_csv.h cpl_auto_close.h ; do /tmp/gdal/gdal/install-sh -c -m 0644 $f /usr/local/include ; done make[1]: Leaving directory `/tmp/gdal/gdal/port' (cd gcore; make install) make[1]: Entering directory `/tmp/gdal/gdal/gcore' for f in gdal_frmts.h gdalgeorefpamdataset.h gdal.h gdaljp2abstractdataset.h gdaljp2metadata.h gdal_mdreader.h gdal_pam.h gdal_priv.h gdal_proxy.h gdal_rat.h rawdataset.h gdal_version.h ; do /tmp/gdal/gdal/install-sh -c -m 0644 $f /usr/local/include ; done make[1]: Leaving directory `/tmp/gdal/gdal/gcore' (cd frmts; make install) make[1]: Entering directory `/tmp/gdal/gdal/frmts' make -C vrt install make[2]: Entering directory `/tmp/gdal/gdal/frmts/vrt' /tmp/gdal/gdal/install-sh -c -m 0644 vrtdataset.h /usr/local/include /tmp/gdal/gdal/install-sh -c -m 0644 gdal_vrt.h /usr/local/include make[2]: Leaving directory `/tmp/gdal/gdal/frmts/vrt' make -C mem install make[2]: Entering directory `/tmp/gdal/gdal/frmts/mem' /tmp/gdal/gdal/install-sh -c -m 0644 memdataset.h /usr/local/include make[2]: Leaving directory `/tmp/gdal/gdal/frmts/mem' make -C raw install make[2]: Entering directory `/tmp/gdal/gdal/frmts/raw' make[2]: Nothing to be done for `install'. make[2]: Leaving directory `/tmp/gdal/gdal/frmts/raw' make[1]: Leaving directory `/tmp/gdal/gdal/frmts' (cd alg; make install) make[1]: Entering directory `/tmp/gdal/gdal/alg' for f in gdal_alg.h gdal_alg_priv.h gdalgrid.h gdalgrid_priv.h gdalpansharpen.h gdal_simplesurf.h gdalwarper.h ; do /tmp/gdal/gdal/install-sh -c -m 0644 $f /usr/local/include ; done make[1]: Leaving directory `/tmp/gdal/gdal/alg' (cd ogr; make install) make[1]: Entering directory `/tmp/gdal/gdal/ogr' for f in ogr_core.h ogr_feature.h ogr_geometry.h ogr_p.h ogr_spatialref.h ogr_srs_api.h ogrsf_frmts/ogrsf_frmts.h ogr_featurestyle.h ogr_api.h ogr_geocoding.h ogr_swq.h ; \ do /tmp/gdal/gdal/install-sh -c -m 0644 $f /usr/local/include ; \ done make[1]: Leaving directory `/tmp/gdal/gdal/ogr' (cd gnm; make install) make[1]: Entering directory `/tmp/gdal/gdal/gnm' for f in gnm.h gnm_api.h gnmgraph.h ; \ do /tmp/gdal/gdal/install-sh -c -m 0644 $f /usr/local/include ; \ done make[1]: Leaving directory `/tmp/gdal/gdal/gnm' (cd apps; make install) make[1]: Entering directory `/tmp/gdal/gdal/apps' for f in gdalinfo gdal_translate gdaladdo gdalwarp nearblack gdalmanage gdalenhance gdaltransform gdaldem gdallocationinfo gdalsrsinfo gdalmdiminfo gdalmdimtranslate gdal_viewshed gdal_create gdal_contour gdaltindex gdal_rasterize gdal_grid ogrinfo ogr2ogr ogrtindex ogrlineref testepsg gdalbuildvrt gnmmanage gnmanalyse ; do /bin/sh /tmp/gdal/gdal/libtool --mode=install --silent /tmp/gdal/gdal/install-sh -c $f /usr/local/bin ; done /tmp/gdal/gdal/install-sh -c -m 0644 gdal_utils.h /usr/local/include /bin/sh /tmp/gdal/gdal/libtool --mode=install --silent /tmp/gdal/gdal/install-sh -c gdal-config-inst /usr/local/bin/gdal-config make[1]: Leaving directory `/tmp/gdal/gdal/apps' (cd swig; make install) make[1]: Entering directory `/tmp/gdal/gdal/swig' for dir in python ; do (cd $dir; make install) || exit; done make[2]: Entering directory `/tmp/gdal/gdal/swig/python' ---------------------------------------------------------------------- The GDAL python package has been installed in lib/python2.7/site-packages Please ensure to add lib/python2.7/site-packages to your PYTHONPATH ---------------------------------------------------------------------- env PYTHONPATH=lib/python2.7/site-packages${PYTHONPATH:+:$PYTHONPATH} \ python setup.py install --single-version-externally-managed --record=record.txt --prefix=/usr/local /usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'extras_require' warnings.warn(msg) /usr/lib64/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'long_description_content_type' warnings.warn(msg) usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] or: setup.py --help [cmd1 cmd2 ...] or: setup.py --help-commands or: setup.py cmd --help error: option --single-version-externally-managed not recognized WARNING: numpy not available! Array support will not be enabled make[2]: Leaving directory `/tmp/gdal/gdal/swig/python' make[2]: *** [install] Error 1 make[1]: Leaving directory `/tmp/gdal/gdal/swig' make[1]: *** [install] Error 2 make: *** [install-actions] Error 2
之前试过GDAL 3.2.2版本,因安装问题切换到3.2.2.1(带安装修复),该版本和开发机一致,也可以尝试更新版本,核心需求是从地理参考TIFF中提取像素的GPS坐标。
错误原因分析
- Python版本绑定错误:镜像中
python命令默认指向Python2.7,GDAL的configure脚本自动绑定到该版本,导致Python绑定安装时调用Python2.7的distutils模块,而--single-version-externally-managed是setuptools扩展的选项,原生distutils不支持。 - Numpy路径不匹配:虽然安装了Python3.9版本的Numpy,但安装GDAL时使用的是Python2.7,无法找到对应版本的Numpy,导致出现"numpy not available"警告。
解决方案
方案1:修改编译配置,绑定Python3.9
修改Dockerfile中GDAL的编译步骤,明确指定Python3.9路径,并设置环境变量确保安装阶段使用正确的Python版本:
# ... 前面的依赖安装、PROJ编译步骤保持不变 ... RUN git clone --branch patch/3.2.2.1 https://github.com/OSGeo/gdal.git && \ cd gdal/gdal && \ # 指定Python3.9路径,让configure绑定目标版本 ./configure --with-proj=/usr/local --with-python=/usr/bin/python3.9 && \ make && \ # 设置环境变量,强制install阶段使用Python3.9 export PYTHON=/usr/bin/python3.9 && \ sudo make install && \ # 更新系统库缓存,确保Python能加载GDAL动态库 sudo ldconfig && \ cd ../.. && \ rm -rf gdal # ... 后续验证步骤保持不变 ...
方案2:直接用pip安装预编译GDAL包(推荐)
跳过手动编译,直接通过pip安装适配Python3.9的预编译GDAL包,更简单可靠:
FROM public.ecr.aws/lambda/python:3.9 # 安装系统依赖库 RUN yum install -y mesa-libGL libtiff proj-devel gdal-libs # 安装GDAL Python绑定(指定与系统库兼容的版本) RUN python3.9 -m pip install numpy==1.22.4 GDAL==3.2.2
若3.2.2版本仍有问题,可尝试更新到更稳定的版本(如GDAL==3.6.4),只需保证与系统安装的gdal-libs版本兼容即可。
安装验证
在Dockerfile末尾添加验证命令,确保GDAL Python绑定正常工作:
RUN python3.9 -c "from osgeo import gdal; print(gdal.__version__)"
内容的提问来源于stack exchange,提问作者Joysn
相关产品推荐
相关产品推荐

