Using the code examples for the processing
module found in the cheatsheet, in the QGIS Python console I can print a list of the algorithms available. I want to do the same from a "standalone" python script for my Windows box. Is it possible?
I made a Windows batch script based on "C:\OSGeo4W64\bin\python-qgis.bat"
(from this answer) to setup the environment:
@ECHO OFF
REM this batch file sets required environment variables to allow pyqgis scripts to work
REM and then runs VS Code in that environment.
set OSGEO4W_ROOT=C:\OSGeo4W64
@echo off
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"
@echo off
path %OSGEO4W_ROOT%\bin;%OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%
cd /d %~dp0
SET VSCBIN=C:\Program Files\Microsoft VS Code\
call "%VSCBIN%\Code.exe" %*
Then after I run that batch script I create a python script in VS Code with the same content as in the cheatsheet:
from qgis.core import QgsApplication
for alg in QgsApplication.processingRegistry().algorithms():
print("{}:{} --> {}".format(alg.provider().name(), alg.name(), alg.displayName()))
But it does not produce any output. It doesn't produce any errors so that's one small bit of progress. This code also doesn't work when run from the python console opened from running the batch file "C:\OSGeo4W64\bin\python-qgis.bat"
.
I can see from other examples here and there that I might also need to init QgsApplication like so:
from qgis.core import QgsApplication
# Supply path to qgis install location
QgsApplication.setPrefixPath("C:\\OSGeo4W64\\apps\\qgis", True)
# (not sure why the above line is needed as the batch file already sets the prefix path)
# init Qgis, but don't create the GUI
qgs = QgsApplication([], False)
qgs.initQgis()
for alg in QgsApplication.processingRegistry().algorithms():
print("{}:{} --> {}".format(alg.provider().name(), alg.name(), alg.displayName()))
But still no output. I get no errors, so I know that the imports must be fine. There just doesn't seem to be any "link" to QGIS itself. What am I doing wrong?