Is there any way to detect if a script is computed on QGIS python console or not?
Any attribute or test to assess it?
-
Isn't this just a dupilcate of your previous question? Is my script executed from a QGIS plugin or from a standard python console? Conditional ImportsJoseph– Joseph2016年01月22日 10:33:56 +00:00Commented Jan 22, 2016 at 10:33
-
1This one is more general, but I'll edit the other one to be distinct, you're right. Both can be useful for different use cases.Nono– Nono2016年01月22日 10:42:11 +00:00Commented Jan 22, 2016 at 10:42
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
answered Jan 22, 2016 at 11:55
-
I found also : from PyQt4.QtCore import * QSettings() returns an error outside QGIS, ty both of you!Nono– Nono2016年01月22日 12:17:31 +00:00Commented Jan 22, 2016 at 12:17
-
1basing 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 confLuigi Pirelli– Luigi Pirelli2016年01月22日 12:38:04 +00:00Commented 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
answered Jan 22, 2016 at 12:00
-
ok... I didn't check that iface is None... tnx @nathanLuigi Pirelli– Luigi Pirelli2016年01月22日 12:01:06 +00:00Commented Jan 22, 2016 at 12:01
lang-py