0

I have been using the Python console in QGIS to develop a script. However, I keep getting the below error whenever I try to execute it.

enter image description here

This is line 43 which has the error enter image description here

2
  • 1
    Did you import QgsGraph? Commented Sep 24, 2023 at 18:15
  • I have tried but it gives me this error. " line 2, in ImportError: cannot import name 'QgsGraph' from 'qgis.core' (C:\PROGRA~1/QGIS32~1.11/apps/qgis-ltr/./python\qgis\core_init_.py) " Commented Sep 24, 2023 at 18:35

1 Answer 1

4

You need to make sure you are correctly importing the QgsGraph class. The first port of call should always be the official documentation.

Both the Python and C++ api docs show clearly that the QgsGraph class belongs to the analysis library so trying to import it from the core library won't work:

enter image description here

enter image description here

Therefore, the correct import statement is:

from qgis.analysis import QgsGraph

Then you can create an instance and work with the class:

graph = QgsGraph()

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.