While I was trying to load a vector layer (Shapefile) from the desktop directory into the QGIS using the iface variable data is not loading and user name in location of the status message is showing strange symbol .Can anyone please suggest the solution.

Code is:


Error Received while loading: enter image description here

the username needs to be like: \Users\bipin162\Desktop\TM_WORLD_BORDERS-0.3.shp

  • Dash - is unaccepted character in shapefile name. – ahmadhanb Mar 11 at 6:54
  • Dash has been removed and renamed with simple name still same error is showing in QGIS staus bar. – Devenepali Mar 11 at 7:00
  • 2
    You need to put r before the path. see the answer. – ahmadhanb Mar 11 at 7:06

Using QGIS 3 you need to load the layers like this:

uri = r"F:\Ahmad\Test\Test4\TestGrid_Test.shp"
iface.addVectorLayer(uri, "Grid", "ogr")

You need to put r before the path. r stands for "raw" which interprets backslash in the string as actual backslash rather than a special character.

You can refer to the tutorial of PyQGIS 101: Loading a vector layer

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.