4

I'm trying to execute the following code on a windows 7 system:

import qgis.core as qc
qgis_path = "C:\Program Files\QGIS Lyon\apps\qgis"
qc.QgsApplication.setPrefixPath(qgis_path, True)
qc.QgsApplication.initQgis()
print "Successfully Initialised"

The qc.QgsApplication.initQgis() command causes python to stop working. Can anyone help with the steps I need to diagnose this problem?

Some background:

  • I've tried a number of different combinations of forward slashes, escaped backslashes etc.for the path and still get the same error;
  • I've been developing the code (part of a larger application) on Linux, without problems;
  • A long time ago, I tested the code on Windows and it worked OK. At that time I had QGIS Dufour installed;
  • I suspect it may have something to do with having the correct environment variables set, but have been unable to find a definitive guide on which and how these variables need to be set.
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
mcminime
  • 41
  • 1
  • 2

1 Answers1

8

It seems things changed to init PyQGIS but were not updated in the cookbook.

There is a discussion about the issue. Look at the bottom of the discussion mainly.

Be careful, it's only a hint. It may be related to your Windows path too (working on Linux mainly).

So, it should work with

from qgis.core import QgsApplication
qgis_path = "C:/Program Files/QGIS Lyon/apps/qgis"
QgsApplication.setPrefixPath(qgis_path, True)
qgs = QgsApplication(sys.argv, False)
qgs.initQgis()
ThomasG77
  • 30,725
  • 1
  • 53
  • 93