10

i try to export a map but at the end of my process I always get the following message: "name 'iface' is not defined".

What is wrong with my script?

I am working with QGIS 2.18.12

from PyQt4.QtGui import QColor, QImage, QPainter
from qgis.core import QgsExpression, QgsStatisticalSummary, QgsSymbolV2,   QgsDataDefined, QgsRuleBasedRendererV2, QgsMarkerSymbolV2
from PyQt4.QtCore import Qsize

...

map_settings = iface.mapCanvas().mapSettings()
c = QgsComposition(map_settings)
c.setPaperSize(400,160)
c.setPrintResolution(dpi)

...
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
  • I didn't test your script, but it seems like an issue with a library import. Try to read my answer. – mgri Sep 18 '17 at 19:26
  • 2
    @radouxju I don't think it is a duplicate because it seems that the OP is not working on a standalone script. – mgri Sep 18 '17 at 19:27
  • @mgri your answer makes sense, but isn't is also valid for the other question. It seemed to me that the OP is working with a standalone app. Maybe you could add an answer to the other question. – radouxju Sep 19 '17 at 05:25

1 Answers1

26

The iface module is automatically imported when directly working from the Python Console.

In the other cases (for example, when running a script from the Processing Toolbox), you need to manually import it in this way:

from qgis.utils import iface
mgri
  • 16,159
  • 6
  • 47
  • 80