4

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

Any attribute or test to assess it?

Nono
  • 473
  • 4
  • 12

2 Answers2

5

You sure can.

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

In QGIS this will return True outside it will return False

Nathan W
  • 34,706
  • 5
  • 97
  • 148
  • 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

Luigi Pirelli
  • 2,013
  • 14
  • 19