title: Python Script with QGis - Python.exe Stopped Working
body: <p>I purchased <a href="https://www.packtpub.com/application-development/building-mapping-applications-qgis" rel="nofollow">this book called Building Mapping Applications with QGIS</a> and I am trying to work through one of the exercises. There is one script that I try to run that crashes python, generating the error message "python.exe has stopped working".</p>
  
  <pre><code>import sys
  import os
  from qgis.core import *
  from qgis.gui import *
  from PyQt4.QtGui import *
  from PyQt4.QtCore import Qt
  
  
  
  #############################################################################
  
  
  class MapViewer(QMainWindow):
      def __init__(self, shapefile):
          QMainWindow.__init__(self)
          self.setWindowTitle("Map Viewer")
  
          canvas = QgsMapCanvas()
          canvas.useImageToRender(False)
          canvas.setCanvasColor(Qt.white)
          canvas.show()
  
          layer = QgsVectorLayer(shapefile, "layer1", "ogr")
          if not layer.isValid():
              raise IOError("Invalid shapefile")
  
          QgsMapLayerRegistry.instance().addMapLayer(layer)
          canvas.setExtent(layer.extent())
          canvas.setLayerSet([QgsMapCanvasLayer(layer)])
  
          layout = QVBoxLayout()
          layout.addWidget(canvas)
  
          contents = QWidget()
          contents.setLayout(layout)
          self.setCentralWidget(contents)
  
  #############################################################################
  
  
  def main():
      """  Our main program.
      """
      QgsApplication.setPrefixPath(os.environ['QGIS_PREFIX'], True)
      QgsApplication.initQgis()
  
      app = QApplication(sys.argv)
  
      viewer = MapViewer("C:/folder/shapefile.shp")
      viewer.show()
  
      app.exec_()
  
      QgsApplication.exitQgis()
  
  #############################################################################
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <p>I don't know a whole lot about Python with QGIS so I'm not too sure what is causing python to crash. I am positive that all of the modules are importing correctly because if I define my paths and then import the modules in the script using the OSGeo4W Shell, there are no error messages.</p>
  
  <p>This is how my paths are defined:</p>
  
  <pre><code>SET OSGEO4W_ROOT=C:\OSGeo4W64
  SET QGIS_PREFIX=%OSGEO4W_ROOT%\apps\qgis
  SET PATH=%PATH%;%QGIS_PREFIX%\bin
  SET PYTHONPATH=%QGIS_PREFIX%\python;%PYTHONPATH%
  </code></pre>
  
  <p>Given all of this, I think there has to be something wrong in the script. However, when I check the script using <a href="http://pep8online.com/" rel="nofollow">http://pep8online.com/</a> there are no errors that I can fix that will result in python not crashing.</p>
  
  <p>Note that I have tried I have tried <code>SET PATH=%QGIS_PREFIX%\bin;%PATH%</code> instead of <code>SET PATH=%PATH%;%QGIS_PREFIX%\bin</code> with no success.</p>