2

I am trying to set up QGIS within a Docker container in order to run standalone Python scripts, but I have a few problems.

  • I first had a No module named PyQt5.sip error which I fixed by adding PyQt5 in my requirements.txt file
  • Then I had a No module named qgis.core which I fixed by adding a whole bunch of paths at the beginning of my script (see below)
  • But now I get a ModuleNotFoundError: No module named 'qgis._core' for which I can't find a fix...

I tried to build a Dockerfile closer to what is recommended here but that seemed to put me further from my goal with libgdal30, libproj22 and many other libraries not being able to be installed. I also have GDAL installed, which might be a reason for these errors.

Dockerfile

# Use an official Python runtime as the base image
FROM python:3.10

RUN  pip install --upgrade pip

# Install QGIS
RUN apt-get update \
    && apt-get install -y python3-launchpadlib \
    && apt-get install -y software-properties-common \
    && apt-get update \
    && apt-get install -y qgis \
    && apt-get install -y qgis-plugin-grass

# Set environment variables for QGIS
ENV QGIS_PREFIX_PATH=/usr

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any necessary Python dependencies from requirements.txt
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt

# Run your Python script
CMD ["python3", "script.py"]

Python script

import sys

sys.path.append('/usr/lib/python310.zip')
sys.path.append('/usr/lib/python3.10')
sys.path.append('/usr/lib/python3.10/lib-dynload')
sys.path.append('/home/timothee/.local/lib/python3.10/site-packages')
sys.path.append('/usr/local/lib/python3.10/dist-packages')
sys.path.append('/usr/lib/python3/dist-packages')
sys.path.append("/usr/lib/python3/dist-packages/qgis")
sys.path.append("/usr/lib/python3/")
sys.path.append('/usr/share/qgis/python/')
sys.path.append('/usr/lib/qgis/')
sys.path.append('/usr/lib/')
import qgis.core
print(qgis.core.Qgis.QGIS_VERSION)

Traceback (most recent call last):
  File "/app/make_tiles_scheduler.py", line 21, in <module>
    from make_tiles import *
  File "/app/make_tiles/__init__.py", line 1, in <module>
    from .make_tiles import make_tiles
  File "/app/make_tiles/make_tiles.py", line 16, in <module>
    import qgis.core
  File "/usr/lib/python3/dist-packages/qgis/core/__init__.py", line 25, in <module>
    from qgis._core import *
ModuleNotFoundError: No module named 'qgis._core'
3
  • 1
    And why not use the official image : hub.docker.com/r/qgis/qgis ? Commented Jul 5, 2023 at 15:34
  • 1
    Official image are too big in my opinion, I'm not using them when I can. (I still use them for some use-case). The 3.28 official image is 8.51GB... My answser is 1.12GB for the 3.28 image.
    – etrimaille
    Commented Jul 5, 2023 at 18:22
  • You can have a look to the dockerfile : github.com/3liz/docker-qgis-platform docker run -it -v $PWD:/scripts 3liz/qgis-platform:3.28 python3 /scripts/script.py will execute the script.py.
    – etrimaille
    Commented Jul 5, 2023 at 18:24

1 Answer 1

2

As suggested, using an official QGIS Docker image works great.

Dockerfile

# Use an official QGIS image
FROM qgis/qgis

# Set environment variables for QGIS
ENV QGIS_PREFIX_PATH=/usr
ENV QT_QPA_PLATFORM=offscreen
ENV XDG_RUNTIME_DIR=/tmp/runtime-root

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any necessary Python dependencies from requirements.txt
COPY requirements.txt /app/
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt

# Run your Python script
CMD ["python3", "script.py"]

docker-compose.yml

  python-script:
    image: qgis:latest
    build:
      context: ./directory
      dockerfile: Dockerfile
    restart: unless-stopped
    env_file:
      - .env

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.