2

I am using QGIS 3.10.2-A Coruña and PyQGIS via the internal Python Console (Plugins -> Python Console). Currently I am trying to import a shape file using the following command:

vlayer = QgsVectorLayer("path/file.shp", "layer_name", "ogr")

It seems to be loaded in the sense that I don't get any error message, I can return the column names (fields) and I can also see the attribute table via:

iface.showAttributeTable(vlayer)

However, the loaded .shp file is neither displayed in the list of layers nor is the corresponding map shown in the main window. I don't have that issue when loading files from a geoPackage or with point vector layers.

What do I have to do differently?

1 Answer 1

4

Are you sure it works by replacing a shapefile with a geopackage?

What you are doing doesn't ask QGIS to add it to the map canvas. You are only creating the python variable vlayer in the memory, representing a vector layer.

After the layer creation, you must add it to the project :

QgsProject.instance().addMapLayer(vlayer)
1
  • Thanks a lot, works! I thought it did with geopackage, but that's just from my notes from a tutorial I did some time ago, so you're probably right and it doesn't without the additional .instance() command.
    – L B
    Commented Mar 18, 2020 at 10:09

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.