0

The following is the only the beginning of my script but this is where the problem is located. It runs fine the first time, but break the second time I try running it. I am running the script directly in the QGIS(3.38) python console. There is not problem with the import part of the code only the code after that and I don't know why. I am getting no error message or anything

import sys
from qgis.core import QgsApplication

# Initialize QGIS Application

    try:
        # Adjust the path to your QGIS installation
        QgsApplication.setPrefixPath("C:/Program Files/QGIS 3.38.0", True)       

#also tried the file path: C:/Program Files/QGIS 3.38.0/apps/qgis
        qgs = QgsApplication([], False)
        qgs.initQgis()
        print("QGIS application initialized successfully.") 
except Exception as e:
        print(f"Error initializing QGIS: {e}")

# Properly exit QGIS application

try:
    qgs.exitQgis()
    print("QGIS application exited successfully.")
except Exception as e:
    print(f"Error exiting QGIS: {e}")
1
  • 1
    This is code meant to be run outside of QGIS, not in its built-in Python console. Commented Jul 2 at 11:23

1 Answer 1

1

As @bugmenot123 commented, this sort of script is intended for use outside of QGIS, but I tested it externally and it still crashed, so:

Try changing:

qgs.exitQgis()

To:

qgs.exit()

I have seen this a few times and this change normally works. I have done a bit of Googling to find an explanation but not found a satisfactory one I'm afraid. The comment at the bottom of this post suggests the same thing: Using exitQgis() in PyQGIS?

1

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.