# QGIS3 call model from windows path with Python script

I was successful in triggering a model with the Python editor with this one-liner:

    processing.runAndLoadResults('model:my_model', {'param1':sParam1,'param2': sParam2,'coordinatesystem':QgsCoordinateReferenceSystem('EPSG:25832')),'qgis:createpointslayerfromtable_1:Trees':'memory:'})


It only works, when the model is already added to the QGIS project. In my scenario this is not the case, which is why I need to replace the part 'model:my_model' with a windows path to the model.

I found some helpful info here:
The example is unfortunately for QgsRasterLayer . I assumed something like "QgsModel" would serve my purpose, so I searched for a class containing "model" in the QGIS API Documentation, but without success.

How do you use processing.runAndLoadResults() with a model stored on disk?

• I am just thinking maybe you have to save your model file/script to a specific qgis folder and use a concrete naming? I am checking this – Ruben FV Jul 19 '19 at 13:18
• Did you save your model as a 'project model' or as a *.model3 file? – Ruben FV Jul 19 '19 at 13:26
• As a .model3 file – GISme Jul 19 '19 at 13:55
• Can you try to save it as part of the project and load the project when executing your python code? – Ruben FV Jul 19 '19 at 14:41
• I am not sure if this helps, did you create your model with the grphical modeler? check my answer – Ruben FV Jul 19 '19 at 14:46

Not a definite solution but one possible method could be to load the model dialog and insert this into the processing call function. The issue here is that the only parameters the user defines would be the input and output layers, all other parameters would use the same values stored in the actual model.

Example:

I have a simple model which takes an input layer, buffers it accordingly to some parameters and outputs the result:

This model is saved on my desktop. If I want to now run the model using processing, I could use:

from processing.modeler.ModelerDialog import ModelerDialog
dlg = ModelerDialog()


Note that the input parameter is the same name as the parameter in the model (layer) and the output is named after the algorithm + the output parameter name (native:buffer_1:result). If you include any other parameters, they are ignored as they only use the values from the model. There probably is a method to run the model and define the parameter values using python so hopefully others can find it.

Make sure your model .model3 file is the default QGIS folder: user folder + \processing\models

You can find this user folder by clicking on Settings / User profiles / Open Active Profile folder

(For example, in my Windows computer)

C:\Users\user\AppData\Roaming\QGIS\QGIS3\profiles\default\processing\models

Maybe you can also try to save the model file as part of a specific project (the option is given in the graphical modeler).

from qgis.core import QgsProject