没有名为“osgeo._gdal_array”的模块-使用Python和GDAL的Docker镜像

0mkxixxg  于 8个月前  发布在  Python
关注(0)|答案(1)|浏览(169)

我在docker中使用GDAL时遇到了问题。我的应用程序在本地,但在docker上时,我得到了这个错误:
ModuleNotFoundError:没有名为“osgeo._gdal_array”的模块
我在这里创建了一个小例子,使其更容易reproductiong错误。这里我的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"]


edit:updated Dockerfile,我注意到有些代码在这个例子中没有做任何事情

gdrx4gfi

gdrx4gfi1#

为了让它工作,我需要在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"]

字符串

相关问题