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. Ok, get to the point...

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? Thanks in advance for yours help!

1
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works – Ian Turton May 25 at 19:40
1

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})
1
  • Thanks a lot for helping me solve my problem. Pyqgis is fairly new to me. I feel more comfortable working with the arcpy library. Importing all the necessary modules in pyqgis is driving me crazy and isn't so obvious. Thank once again! Cheers! – nomad_gis May 26 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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