1

Setup is QGIS version is 3.28 on windows 11 with project property Save Paths set to relative.

I am following the instructions in the PyQGIS cookbook for adding layers using this code:

shp_filename= "C:/Users/anon/OneDrive/QPythonDev/pyqgis3_files/countries.shp"
vlayer = QgsVectorLayer(shp_filename, "countries", "ogr")
QgsProject.instance().addMapLayer(vlayer

I can add a layer from the project folder or any other folder only when the full path is supplied. I cannot add with this (which is suggested in the documentation)

shp_filename = "data/countries.shp"

When the project is saved and reopened, the shapefile in the project folder loads - it is located in a sub-folder 5 folders in from the root: C:/Users/anon/OneDrive/QPythonDev/pyqgis3_files/countries.shp

If I add a shapefile from a location such as C:\temp, that file is not found - this is the data source location given: ../../../../../../../temp/world.shp

If the project properties setting Save Paths is set to absolute, the files are found and loaded correctly. My question is - is there a limit to folder nesting when you have Save Paths set to relative? And why does the relative file location "data/countries.shp" not work.

0

Your Answer

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

Browse other questions tagged or ask your own question.