2

I've recently started working with Python in QGIS and I've been trying to make a script that dissolves a polygon layer and saves the output in the same folder as the input, something I believe isn't possible to do with QGIS Graphical Modeller.

The code I've written is as follows:

processing.run("native:dissolve", {'INPUT':iface.activeLayer(),'FIELD':[],'OUTPUT':iface.activeLayer().dataProvider().dataSourceUri().split('2D', 2)[0] +"2D Simulation Elements dissolved.shp"})
iface.addVectorLayer((iface.activeLayer().dataProvider().dataSourceUri().split('2D', 2)[0] +"2D Simulation Elements dissolved.shp"),"","ogr")

It works fine in the console, however when I put it in the Script Toolbox it fails with the following error:

Traceback Error

The code is confusing me cause, I obviously don't have 224 lines of code for it have an error there... I've looked online and followed the suggestion to add the following on top of my code:

QgsApplication.initQgis()
app = QgsApplication([], True)

But it didn't make my script work as well.

1
  • 1
    you need import QgsApplication using from qgis.core import QgsApplication
    – Fran Raga
    Commented Jul 29, 2019 at 11:54

1 Answer 1

2

when trying to run a script outside of the python console you must define iface and processing by adding

from qgis.utils import iface
from qgis.core import  (QgsProcessing)

import processing

Should run now outside the python console from the processing toolbox

1
  • It works, but i am getting an error when i ran it, but i am happy as long as it works. Commented Jul 29, 2019 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.