5

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

Any attribute or test to assess it?

asked Jan 22, 2016 at 10:13
2

2 Answers 2

6

You sure can.

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

In QGIS this will return True outside it will return False

answered Jan 22, 2016 at 11:55
2
  • I found also : from PyQt4.QtCore import * QSettings() returns an error outside QGIS, ty both of you! Commented 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 Commented Jan 22, 2016 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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.