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

Etienne Trimaille etienne.trimaille at gmail.com
Wed Jul 5 05:45:20 PDT 2023


Hi,

I'm not answering straight the question, but you can have a look to the
Dockerfile on our working base image :
https://github.com/3liz/docker-qgis-platform/

As an example :

➜ cat script.py
import qgis.core
print(qgis.core.Qgis.QGIS_VERSION)

➜ docker run -it -v $PWD:/scripts 3liz/qgis-platform:3.28 python3
/scripts/script.py
3.28.7-Firenze



Le mer. 5 juil. 2023 à 14:20, Timothée Billiet Cadart via QGIS-Developer <
qgis-developer at lists.osgeo.org> a écrit :

> 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'
>
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20230705/62ad0f00/attachment-0001.htm>


More information about the QGIS-Developer mailing list