Monday, June 5, 2023

Using PyScripter with QGIS

This is a solution contributed by Andreas (github user name amr).  For a complete integration of PyScripter with QGIS you need two things:

  1. Batch-File: The following is for the LTR-version of QGIS. It is a modified version of the existing python-qgis-ltr.bat in the folder "OSGeo4W\bin". Some changes were made to it, essentially using absolute paths to both the OSGeo4W and PyScripter installation folders. Please change the paths, if yours differ!
  2.  

    @echo off
    :: path for OSGeo4W/QGIS
    set O4W=C:\OSGeo4W
    :: path for PyScripter
    set PYSC=C:\Program Files\PyScripter
    
    :: changed start procedure
    call "%O4W%\bin\o4w_env.bat"
    @echo off
    :: add python dir to path
    path %OSGEO4W_ROOT%\apps\qgis-ltr\bin;%OSGEO4W_ROOT%\apps\Python39;%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%
    
    :: start PyScripter
    start "PyScripter" /B "%PYSC%\PyScripter.exe" --PYTHON39 --pythondllpath=%OSGEO4W_ROOT%\apps\python39 %*
    
    
  3. Start-Script for the Python-Interpreter: It uses the path variable and adds each path to the DLL-search path. Store this as "sitecustomize.py" into the folder OSGeo4W\apps\Python39
#sitecustomize.py
import os
for p in os.getenv("PATH").split(";"):
    if os.path.exists(p):
        os.add_dll_directory(p)