4

I am using QGIS 3.4 but it does not have the option of saving a model to a Python script. So I installed QGIS 3.8, which has that option, loaded my model and saved it as a Python script. I loaded the script into the Python editor, clicked "run" and expected the script to work in Version 3.8, but it didn't. The Python console just gives back

exec(open('P:/.../QGIS_Import.py'.encode('utf-8')).read())

With loading it as a script it worked in QGIS 3.8: The model and the python script do not generate the same results, however, that is not the real problem. When I run the python script in QGIS 3.4 it gives this error:

Traceback (most recent call last):
File "C:\Users\heubacha\AppData\Roaming\QGIS\QGIS3\profiles\default\processing\scripts\__QGIS_3.8_Import_Original.py", line 73, in processAlgorithm
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
AttributeError: type object 'QgsProcessing' has no attribute 'TEMPORARY_OUTPUT'

The code might not be downwards compatible...what is the correct coding in QGIS 3.4? This is the code snippet where the error occurs:

alg_params = {
            'DISCARD_NONMATCHING': True,
            'FIELD': 'STP',
            'FIELDS_TO_COPY': None,
            'FIELD_2': 'OZ2',
            'INPUT': outputs['FbRefactorFields']['OUTPUT'],
            'INPUT_2': parameters['formstp'],
            'METHOD': 0,
            'PREFIX': '',
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['FbfsJoinAttributesByFieldValue'] = processing.run('native:joinattributestable', alg_params, context=context, feedback=feedback, is_child_algorithm=True)

        feedback.setCurrentStep(50)
        if feedback.isCanceled():
            return {}

1 Answer 1

1

The Export to script tool (from a model) does not create a "runnable" script. Instead, it creates a script that can be imported as a tool into Processing. At first sight, it seems quite useless, after all, you get the exact same tool, but since it's now in python, you can tweak it directly on the conde. For example, you can enter conditions and loops, something you can do in model builder.

5
  • Perfect! That is exactly what I wanted! Thank you so much, you have no idea :)
    – GISme
    Jul 5, 2019 at 10:28
  • The model and python script run in QGIS 3.8, but the coding doesn't work in QGIS 3.4. I edited my post with the error message.
    – GISme
    Jul 5, 2019 at 14:57
  • If want to it to run in QGIS 3.4, you need to 'clean' it, remove specific Processing statements which are valid after 3.6. Keep pure python and keep simple processing.run statement. Can you add the python generated in your post?
    – etrimaille
    Jul 5, 2019 at 15:32
  • 1
    For instance QgsProcessing.TEMPORARY_OUTPUT has been introduced in QGIS 3.6. So you need to remove this and manage yourself this part. So you will be able to run it in 3.4.
    – etrimaille
    Jul 5, 2019 at 15:34
  • 2
    There is hope, thanks! I edited my post and added the code snippet where the error occurs. What would the equivalent look like in 3.4?
    – GISme
    Jul 5, 2019 at 18:23

Your Answer

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

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