我在 docker 中使用 GDAL 时遇到问题。 我的应用程序在本地,但在 docker 上时出现此错误:
ModuleNotFoundError:没有名为“osgeo._gdal_array”的模块
我在这里创建了一个小示例,以便更轻松地重现错误。 这是我的
app.py
:
from osgeo import gdal
filename = "test_image.tiff"
ds = gdal.Open(filename)
array = ds.GetRasterBand(1).ReadAsArray()
print(f"array of {len(array)}")
这是我的
requirements.txt
:
GDAL==3.4.3
netCDF4==1.6.2
netifaces==0.11.0
numpy==1.23.5
Werkzeug==2.2.2
还有我的
Dockerfile
:
FROM python:3.11
RUN apt-get update
RUN apt-get install -y gdal-bin libgdal-dev
RUN pip install --global-option=build_ext --global-option="-I/usr/include/gdal" GDAL==3.4.3
ENV CPLUS_INCLUDE_PATH=/usr/include/gdal
ENV C_INCLUDE_PATH=/usr/include/gdal
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
WORKDIR /app
COPY . /app
CMD ["python3", "app.py"]
编辑:更新了 Dockerfile,我注意到有些代码对此示例没有执行任何操作
为了使其工作,我需要在requirements.txt之后再次安装numpy和gdal。
所以这个 docker 文件修复了它:
FROM python:3.11
RUN apt-get update
RUN apt-get install -y gdal-bin libgdal-dev
ENV CPLUS_INCLUDE_PATH=/usr/include/gdal
ENV C_INCLUDE_PATH=/usr/include/gdal
COPY requirements.txt .
RUN python -m pip install -r requirements.txt
#this last 2 after the fixed it
RUN pip install numpy
RUN pip install gdal==$(gdal-config --version)
WORKDIR /app
COPY . /app
CMD ["python3", "app.py"]