Is there any way to detect if a script is computed on QGIS python console or not?

Any attribute or test to assess it?


2 Answers 2


You sure can.

import qgis.utils
inqgis = qgis.utils.iface is not None

In QGIS this will return True outside it will return False

  • I found also : from PyQt4.QtCore import * QSettings() returns an error outside QGIS, ty both of you!
    – Nono
    Jan 22, 2016 at 12:17
  • 1
    basing on QSettings is not a correct solution... you script can be executed in a QApplication that is not qgis and it will return the appliaciton conf Jan 22, 2016 at 12:38

I didn't check, try this:

A) from qgis.utils import iface

  • can generate importError exception in case not initQgis with correct prefix => outside qgis!

B) iface.mainWindow() == None

  • if not error in A => you can be in a qgis standalone app => then you verify if a mainwindow has been ininitialized

giev us a feedbak

  • ok... I didn't check that iface is None... tnx @nathan Jan 22, 2016 at 12:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.