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.
-
1Did you import QgsGraph?Ian Turton– Ian Turton2023-09-24 18:15:57 +00:00Commented 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) "Alaa Torkey– Alaa Torkey2023-09-24 18:35:55 +00:00Commented Sep 24, 2023 at 18:35
Add a comment
|
1 Answer
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:
Therefore, the correct import statement is:
from qgis.analysis import QgsGraph
Then you can create an instance and work with the class:
graph = QgsGraph()



