I try to toggle labels on/off for a layer in QGIS 2.18 with PyQGIS. In PyQGIS 3 a method
QgsVectorLayer().setLabelsEnabled() seem to have been added, but as far as I see, such is not available in PyQGIS 2 (There is no such hint Since QGIS 3.0 as well in the doku for PyQGIS 3 https://qgis.org/api/classQgsVectorLayer.html#a4cdece41437b32f4adafeb1caa10ad1b).
My first try was to access the labeling configuration of the layer with sth. like
label_settings = my_layer.labeling().settings() label_settings.enabled = not label_settings.enabled my_layer.setLabeling(label_settings)
setLabeling() methods are not available in the python bindings (e.g. https://qgis.org/api/2.18/classQgsVectorLayer.html#ada12f21c42e2f3c2eaa2b234ff25ed28).
There is a method
QgsVectorLayer().enableLabels() as well, but this is deprecated (https://qgis.org/api/2.18/classQgsVectorLayer.html#ae7735460da1518febff85575851b8e88) and seems to have no effect.