3

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

Any attribute or test to assess it?

4

You sure can.

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

In QGIS this will return True outside it will return False

|improve this answer|||||
  • I found also : from PyQt4.QtCore import * QSettings() returns an error outside QGIS, ty both of you! – Nono Jan 22 '16 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 – Luigi Pirelli Jan 22 '16 at 12:38
2

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

|improve this answer|||||
  • ok... I didn't check that iface is None... tnx @nathan – Luigi Pirelli Jan 22 '16 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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