[QGIS-Developer] QGIS Python scripts in a Docker container

Timothée Billiet Cadart billiet.timothee at gmail.com
Wed Jul 5 05:20:22 PDT 2023


Hello everyone,

I am trying to setup 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
<https://www.qgis.org/en/site/forusers/alldownloads.html#debian-ubuntu> 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.

Thank you very much for your help
Dockerfile

# Use an official Python runtime as the base imageFROM 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 /appCOPY . /app

# Install any necessary Python dependencies from requirements.txtCOPY
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.coreprint(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'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230705/d2e4f412/attachment-0001.htm>


More information about the QGIS-Developer mailing list