1

Some of the external QGIS plugins contain processing algorithms integrated by the processing toolbox after installation, e.g. the plugin "Shape Tools".

How can the processing algorithms from external plugins be run from PyQGIS standalone scripts?

import sys

from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback, QgsProcessingRegistry

# Supply path to qgis install location 
QgsApplication.setPrefixPath("C:/Program Files/QGIS3.34.4/apps/qgis-ltr", True)

# Create a reference to the QgsApplication.  Setting the
# second argument to False disables the GUI. 
qgs = QgsApplication([], False)

# Load providers 
qgs.initQgis()

import processing 
from processing.core.Processing import Processing 
Processing.initialize()    

# Add the user profile plugins path
sys.path.append("C:/Users/<user>/AppData/Roaming/QGIS/QGIS3/profiles/default/python/plugins")

# Import the shapetools provider 
try:
    from shapetools.provider import ShapeToolsProvider 
except ImportError:
    print("ShapeToolsProvider not found in shapetools")

# Add the processing algorithms
#QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())


#Initialize the shapetools 
QgsApplication.processingRegistry().addProvider(ShapeToolsProvider())

running in cmd gets the error:

Traceback (most recent call last):   File "C:\gis\scripts\test_prod_standalone.py", line 63, in <module>
    buffer_shape_tools = processing.run("shapetools:createcircle",   File "C:\Program Files\QGIS
3.34.4\apps\qgis-ltr\python\plugins\processing\tools\general.py", line 109, in run
    return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context)   File "C:\Program Files\QGIS
3.34.4\apps\qgis-ltr\python\plugins\processing\core\Processing.py", line 158, in runAlgorithm
    raise QgsProcessingException(msg)
_core.QgsProcessingException: Error: Algorithm shapetools:createcircle not found

It seems the shapetools algorithm is not initialized. Git plugin repo see: Git repo shapetools plugin

1 Answer 1

0

I have QGIS 3.4 installed in an anaconda virtual environment. From the QGIS GUI I installed the ShapeTools plugin, which worked correctly as shown in the next figure that shows in the red boxes the Shape Tools tool bar and the list of installed plugins:

enter image description here

The QGIS user directory with the plugins correctly shows the Shape Tools folder:

enter image description here

I then took your script and slightly modified it to adapt it to my local configuration:

import sys

from qgis.core import QgsApplication, QgsVectorLayer, QgsProcessingFeedback, QgsProcessingRegistry


if __name__ == "__main__":
    # Create a reference to the QgsApplication.  Setting the
    # second argument to False disables the GUI. 
    qgs = QgsApplication([], False)

    # Load providers 
    qgs.initQgis()

    import processing 
    from processing.core.Processing import Processing 
    Processing.initialize()

    # Add the user profile plugins path
    sys.path.append(r"c:\Users\<user_name>\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins")

    # Import the shapetools provider 
    try:
        from shapetools.provider import ShapeToolsProvider 
    except ImportError:
        print("ShapeToolsProvider not found in shapetools")

    #Initialize the shapetools 
    QgsApplication.processingRegistry().addProvider(ShapeToolsProvider())

    qgs.exitQgis()

Finally, I can run the script from the command line and I do not get any error:

enter image description here

Given that the code you provided seems to work fine, I would suggest to review the configuration of running standalone PyQGIS scripts from python. Possibly useful post.

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.