3

I am having a hard time loading a vectorlayer from the console. Not from the Pythonconsole within QGIS, but from the console on my computer. I later want to write a standalone script!

That is what I am doing

import os
import sys
# where is the module
sys.path.append("/Applications/QGIS.app/Contents/Resources/python/")
from qgis.analysis import *
from qgis.core import *
from PyQt4.QtCore import *

qpp = QgsApplication([], True)
QgsApplication.setPrefixPath(r"/Application/Qgis.app/Contents/PlugIns", True)
QgsApplication.initQgis()

Then I simply to this

vLayer = QgsVectorLayer("/PATH/TO/SHP/test.shp", "test", "ogr")

When I type vLayer I get

qgis._core.QgsVectorLayer object at 0x110735c30

When I try this vLayer.isValid() I get

False

Any ideas why?

1

May be the path ? because

import sys
sys.path.append('/Applications/QGis.app/Contents/Resources/python/')
sys.path.append('/Applications/QGis.app/Contents/Resources/python/plugins')
from qgis.core import *
app = QgsApplication([],True)
QgsApplication.initQgis()
vLayer = QgsVectorLayer("/Users/Shared/test.shp", "test", "ogr")
vLayer.isValid()
True

Works

  • Nope. I still get False – Stophface Mar 11 '16 at 9:21
1

Thats what you need to add:

import sys
# where is the module
sys.path.append("/Applications/QGIS.app/Contents/Resources/python/")
sys.path.append('/Applications/QGIS.app/Contents/Resources/python/plugins')

qpp = QgsApplication([], True)
QgsApplication.setPrefixPath(r"/Applications/QGIS.app/Contents/PlugIns", True)
QgsApplication.initQgis()

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.