2

I am trying to create a standalone application of QGIS in PyCharm. My code is:

app = QgsApplication([], True)
app.setPrefixPath("usr/lib/qgis/plugins", True)
app.initQgis()
canvas = QgsMapCanvas()
canvas.setWindowTitle('Aeee')
canvas.enableAntiAliasing(True)
canvas.setCanvasColor(Qt.white)
layer = QgsVectorLayer('LineString?crs=epsg:4326', 'MyLine' ,"memory")
pr = layer.dataProvider()
linstr = QgsFeature()
geom = QgsGeometry.fromWkt("LINESTRING (1 1, 10 15, 40 35)")
linstr.setGeometry(geom)
pr.addFeatures([linstr])
layer.updateExtents()
QgsProject.instance().addMapLayer(layer)
canvas.setExtent(layer.extent())
canvas.setLayers([layer])
canvas.zoomToFullExtent()
canvas.freeze(True)
canvas.show()
canvas.refresh()
canvas.freeze(False)
canvas.repaint()
exitcode = app._exec()
QgsApplication.exitQgis()

And the error I get is:

QStandardPaths: XDG_RUNTIME_DIR points to non-existing path '/run/user/1000/snap.pycharm-professional', please create it with 0700 permissions.
QStandardPaths: XDG_RUNTIME_DIR points to non-existing path '/run/user/1000/snap.pycharm-professional', please create it with 0700 permissions.
Process finished with exit code 1

I supposed this problem is because of the application that is trying to access the qgis path and that it having a problem with privileges. My first clue is to change the folder permissions and ownership, but because I am new to Ubuntu and PyQGIS, I asked this question.

bugmenot123
  • 11,011
  • 3
  • 34
  • 69
Neven
  • 526
  • 3
  • 15

2 Answers2

5

The artwokr21's answer has been approved. I just want to add some information for Ubuntu user

For Windows:

QgsApplication.setPrefixPath("C:/Program Files/QGIS 2.18", True)
qgs = QgsApplication([], False)
qgs.initQgis()

For Ubuntu (based on this page):

QgsApplication.setPrefixPath("/usr", True)
qgs = QgsApplication([], False)
qgs.initQgis()
Ayato
  • 162
  • 1
  • 10
2

The prefix path needs to point to the root install directory of QGIS. For example, for windows it may look like this (for v2.18):

QgsApplication.setPrefixPath("C:/Program Files/QGIS 2.18", True) # not sure what Ubuntu path is
qgs = QgsApplication([], False)
qgs.initQgis()
artwork21
  • 35,114
  • 8
  • 66
  • 134