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'
docker run -it -v $PWD:/scripts 3liz/qgis-platform:3.28 python3 /scripts/script.py
will execute thescript.py
.