pythonpython-3.xgdalosgeogdal-python-bindings

No module named 'osgeo._gdal_array' - Docker image with Python and GDAL


I'm having trouble using GDAL in docker. My application locally but when on docker I get this error:

ModuleNotFoundError: No module named 'osgeo._gdal_array'

I created here a small example to make it easier reproductiong the error. here my 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)}")

here my requirements.txt:

GDAL==3.4.3
netCDF4==1.6.2
netifaces==0.11.0
numpy==1.23.5
Werkzeug==2.2.2

and my 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, I noticed that some code was not doing anything for this example


Solution

  • To make it work I needed to install again numpy and gdal after the requirements.txt.

    So this docker file fixes it:

    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 "pip install" fixed the build
    RUN pip install numpy
    RUN pip install gdal==$(gdal-config --version) 
    
    WORKDIR /app
    COPY . /app
    
    CMD ["python3", "app.py"]