1

I'm trying write simple code line in PyQgis in PyCharm, have already set environment in PyCharm and seems to be working fine because I can iterate through f.i fields in vector layer proceed with success simpler operations.

from qgis.core import QgsApplication
from qgis.core import QgsVectorLayer
import processing

polygonLayer = QgsVectorLayer(r'E:\Folder\layer.shp','','ogr')
out_fn =r'E:\Folder\out_layer.shp'
dissolve_field = ['_test_field']

processing.runAndLoadResults('gdal:dissolve',
                             {'INPUT':polygonLayer,
                              'FIELD':dissolve_field,
                              'OUTPUT':out_fn})

After running this few lines i get:

Traceback (most recent call last):
  File "C:\OSGEO4~1\apps\Python37\lib\site-packages\IPython\core\interactiveshell.py", line 3427, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-10-35a681b9a5e0>", line 4, in <module>
    'OUTPUT':out_fn})
  File "C:\OSGeo4W64\apps\qgis\python\plugins\processing\tools\general.py", line 138, in runAndLoadResults
    for param in alg.parameterDefinitions():
AttributeError: 'NoneType' object has no attribute 'parameterDefinitions'

Software set: Pycharm 2020.3.5; QGIS 3.16, installed via OSGEO4W

Could you tell me what i'm doing wrong?

0

1 Answer 1

2

It looks like you are trying to use the processing module in a standalone script. You are missing some code to initialize a QgsApplication instance, as well as importing and initializing the processing module. Please try the example below for running QGIS processing algorithms in a standalone script which I have tested with my own file paths. Also, you should use processing.run() instead of runAndLoadResults() since you are not using QGIS gui.

import sys

from qgis.core import QgsApplication 

from qgis.analysis import QgsNativeAlgorithms

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

# Append the path where processing plugin can be found
sys.path.append('C:\\OSGeo4W64\\apps\\qgis\\python\\plugins')

import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

input = 'E:\\Folder\\layer.shp'
output = 'E:\\Folder\\out_layer.shp'
dissolve_field = '_test_field'

processing.run("gdal:dissolve", {'INPUT':input,
                'FIELD':dissolve_field,
                'OUTPUT':output})
0

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.