I have two *.qgs files created using QGIS 3.0.2. one of them is absolutely new (empty) project and another one is the same as empty but with blank layout.

Here you can download my *.qgs files.

I tried to open them with PyQGIS - the empty one reads fine, the one with blank layout crashes the script with an error:

Create instance...OK project.fileName= 
Reading project...OK project.fileName= C:/QGIS_TEST/empty_project.qgs

Create instance...OK project.fileName= 

Process finished with exit code -1073741819 (0xC0000005)

What can cause the problem?

Here is the code:

import qgis.core

def main(project_path):
    print('Clearing...', end='')

    print('Create instance...', end='')
    project = qgis.core.QgsProject.instance()
    print('OK ', end='')
    print('project.fileName=', project.fileName())

    print('Reading project...', end='')
    print('OK ', end='')
    print('project.fileName=', project.fileName())

if __name__ == "__main__":
    empty_project = r"C:\QGIS_TEST\empty_project.qgs"
    project_with_empty_layout = r"C:\QGIS_TEST\project_with_empty_layout.qgs"

    print('\n empty_project:')

    print('\n project_with_empty_layout:')

  • This 1073741819 error seems to be related to windows 10 – Albert May 22 '18 at 7:21
  • Similar behaviour on Ubuntu - segmentation fault on trying to load the empty project file. Note these load into a running QGIS okay, so perhaps the python code isn't doing sufficient initialisation... – Spacedman May 22 '18 at 7:22
  • @Spacedman But empty project loads well. – Comrade Che May 22 '18 at 7:25
  • @Albert I use windows 7. – Comrade Che May 22 '18 at 7:25

Create a QgsApplication object in your code. Adding this one line to your main fixes this:

def main(project_path):

   qgs = qgis.core.QgsApplication([], False)

why does it work for the empty project? Probably just luck - Qgis isn't doing as much when it gets an empty project file than when it gets the one with the layout, so its not hitting the problem. But I would always create a QgsApplication object.

PyQGIS tutorials also suggest you need:

# load providers

which might apply if you are trying to load data via providers.

  • QgsApplication helped. Also setting the following environment variable QT_QPA_PLATFORM_PLUGIN_PATH=%QTDIR%\plugins\platforms\ worked for me to handle an error This application failed to start because it could not find or load the Qt platform plugin windows. Reinstalling the application may fix this problem. – Comrade Che May 22 '18 at 9:10

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.