Error with PyScripter and QGIS 3.22, can’t find DLL

osgeo4wpyqgispyqt5pyscripterqgis

Since using the new installer for the x64 only version OSGeo4W/QGIS (new 3.22 LTR), I can't no longer use PyScripter with the bundled PyQt5. When I try to import QSettings for example, PyScripter can't find the needed DLL for QtCore (Qt5Core.dll in apps\qt5). When I do the same on the OSGeo4W shell, python imports without any problems. I use similar batch files to start python and PyScripter based on the bin\python-qgis-ltr.bat file:

@echo off
call "%~dp0\bin\o4w_env.bat"
@echo off
path %OSGEO4W_ROOT%\apps\qgis-ltr\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis-ltr
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis-ltr\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis-ltr\python;%PYTHONPATH%
pause
cd %~dp0\PyScripter
start "PyScripter" /B "PyScripter.exe" --PYTHON39 --PYTHONDLLPATH=%OSGEO4W_ROOT%\apps\Python39

my test.py:

from PyQt5.QtCore import QSettings

As I said, Python successfully imports QSettings while PyScripter does give me this error:

>>> from PyQt5.QtCore import QSettings
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ImportError: DLL load failed while importing QtCore: Das angegebene Modul wurde nicht gefunden.

So, I don't know what I am missing, it used to work for QGIS <= 3.16.

Best Answer

I found another workaround: because setting the DLL search path automatically changed in Python 3.8, you have to do this explicitly in your script. I use a start-script for the Python-Interpreter named "sitecustomize.py" which is stored under "OSGeo4W\apps\Python39". It uses the path variable and adds each path to the DLL-search path.

#sitecustomize.py
import os
for p in os.getenv("PATH").split(";"):
    if os.path.exists(p):
        os.add_dll_directory(p)
Related Question