I've installed the qgis on ubuntu 12.04 following the instructions on http://hub.qgis.org/projects/quantum-gis/wiki/Download. I am trying to test the Python API and try to do the follwoing

from qgis.core import *
layer = QgsVectorLayer("coast.shp", "coast_shp", "ogr")

But layer.isValid() returns false. Any ideas why it might happen?

  • do you also have the .shx file for it? Commented Jun 23, 2012 at 15:50
  • The message log (click /!\ at the end of the status bar) might have more information.
    – jef
    Commented Jun 23, 2012 at 18:29
  • 1
    I have the same problem. If I load my .shp file in QGIS, it loads up nicely with all attributes; yes, the shx file is there. I have tried specifying the full path for the .shp file, even though it's local. No effect. Where is the message log? There is no error output, it seems, from this command. ((Oops, this isn't an answer. Should I have made this a comment/reply?))
    – CPBL
    Commented Jul 10, 2012 at 20:33
  • Vadim, there are three versions of installs for Ubuntu listed on the page you noted: release, master, ubuntugis. Which did you use?
    – dakcarto
    Commented Jul 10, 2012 at 23:03

2 Answers 2


I had similar issue.

Found that the problem could be due to the following possibilities:

  1. When you set up the QgsApplication, the PrefixPath must be done correctly.

    My QGIS was installed on /usr/share/qgis.

    But somehow I need to set the PrefixPath as "/usr" & not "/usr/share/qgis":

    QgsApplication.setPrefixPath("/usr", True)
  2. The path of the .shp files.

    Assuming they are under a sub-directory from current path, "Maps/", you can load the file with:

    mapFile = "Maps/world_borders.shp"
    layer = QgsVectorLayer(os.path.abspath(mapFileName), mapFileName[mapFileName.rfind("/")+1:-4], "ogr")
  3. Lastly, not all .shp files are created 'equal'. ;P

    Some .shp file can be loaded without problem into QGIS & displayed.

    Some, probably created in ESRI ArcMap, require the rest of the files (.shp, .shx, .dbf, .prj, .sbn, .sbx)

Hope this info help.


i think you need to give fullpath of your shapefile in windows

layer = QgsVectorLayer("c:/coast.shp", "coast_shp", "ogr")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.