Bug report #19111

Processing step is ignored in QGIS3 standalone, non-GUI python script

Added by Anantha Prasad over 2 years ago. Updated over 2 years ago.

Status:Closed
Priority:Normal
Assignee:-
Category:Processing/QGIS
Affected QGIS version:3.0.3 Regression?:No
Operating System:Windows 10 Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:26941

Description

The standalone python script ignores the processing.run() line below. The output prints "Finished importing", "Starting zonalstats" but never prints "Finished zonalstats". The very same script works very well inside the python console in Qgis3. I substituted the path in the python console into my standalone script in the hope that it had something to do with it not finding the correct path...but it still does not work. I also tried several combinations by changing the script as per instructions in various forums, with no success. So, it seems like a bug.

from qgis.core import (
     QgsApplication, 
     QgsProcessingFeedback, 
     QgsVectorLayer
)

from qgis.core import QgsApplication

# See https://gis.stackexchange.com/a/155852/4972 for details about the prefix 
QgsApplication.setPrefixPath('e:/Qgis3/apps/qgis', True)
qgs = QgsApplication([], False, None)
qgs.initQgis()

# Append the paths inside the python console in Qgis3
sys.path.append('e:\\QGIS3\\apps\\qgis\\qtplugins')
sys.path.append('e:\\QGIS3\\apps/Qt5\\plugins')
sys.path.append('e:\\QGIS3\\apps\\qgis\\python')
sys.path.append('e:\\QGIS3\\apps\\qgis\\python\\plugins')
sys.path.append('E:\\Qgis3\\apps\\Python36\\DLLs')
sys.path.append('E:\\Qgis3\\apps\\Python36\\lib')
sys.path.append('E:\\Qgis3\\apps\\Python36\\lib\\site-packages\\win32')
sys.path.append('E:\\Qgis3\\apps\\Python36\\lib\\site-packages\\win32\\lib')
sys.path.append('E:\\Qgis3\\apps\\Python36\\lib\\site-packages\\Pythonwin')
sys.path.append("e:\\QGIS3\\apps\\Python36\\Lib\\site-packages")
# Append location of DLLs to current system PATH envrionment variable
# os.environ['PATH'] += r";C:\Program Files\QGIS 2.14\apps\qgis-ltr\bin;" 

from processing.core.Processing import Processing
Processing.initialize()
print("Finished importing")
params = { 
    'INPUT_RASTER':'E:\\arp\\LEAP\\s823_actfilt.tif',
    'RASTER_BAND':1,
    'INPUT_VECTOR':'E:\\arp\\LEAP\\test.shp',
    'COLUMN_PREFIX':'actfilt_',
    'STATS':[0,1,2],
}
feedback = QgsProcessingFeedback()
print("Starting zonalstats")
processing.run("qgis:zonalstatistics", params)
print("Finished zonalstats")
QgsApplication.exitQgis()
app.exit()

Associated revisions

Revision 40a2062e
Added by Nyall Dawson over 2 years ago

[python] Don't install error hook by default

This error hook should only ever be used from QGIS app, never
from standalone scripts and applications, so we should default
to not using it and only install it when initializing python
from app.

Otherwise default behavior for standalone scripts based on
PyQGIS is to silently swallow exceptions - this leaves script
developers no clues to go off to debug their applications,
meaning that errors which would usually take a couple of seconds
to fix become horrible exercises in frustration for those
unaware of QGIS' exception handling and the
QGIS_DISABLE_MESSAGE_HOOKS environment variable.

Refs #19111

History

#1 Updated by Nyall Dawson over 2 years ago

  • Resolution set to invalid
  • Status changed from Open to Closed

You're missing an "import processing" call here.

Also available in: Atom PDF