I would like to run a Python script via qgis --code myscript.py
and then immediately exit. I'm using iface.actionExit().trigger()
, which kills QGIS when I run it from the Python console, but not when put in the script that I pass to --code
.
What's the right way to immediately exit? I'm running QGIS 2.0.1
I've also tried sys.exit()
. QGIS catches it and pops up a window with the following:
An error occured during execution of following code:
execfile('myscript.py')
Traceback (most recent call last):
File "", line 1, in
File "myscript.py", line 14, in
sys.exit()
SystemExit
os.kill(os.getpid(), 9)
works but it's a dirty hack and I'm looking for something better.
3 Answers 3
Try in your script:
from qgis.utils import iface
#your code here
iface.actionExit().trigger()
It works for me.
Editing Note:
Based in Conley Owens'comment, I changed slightly my script to do "something useful" (where the line that import iface was commented).
#from qgis.utils import iface
import os
os.system('clear')
print "Hello"
iface.actionExit().trigger()
I ran qgis --code myscript.py at the bash console, inside the folder of the script, and immediately I got this error message:
and indicating that from qgis.utils import iface line is necessary.
When the first line is not commented the script execution had not errors:
but the control was not in the Python console; it is in the bash console (observe the "Hello" print there). For this reason your os.kill(os.getpid(), 9) command works because close the console and automatically close QGIS.
The solution to this issue, if I need the PyQGIS API outside QGIS, it was to include the PYTHONPATH to QGIS (/usr/share/qgis/python) in my .bashrc and to run the script in the bash console as python myscript.py. It works.
In Windows, you can get the PYTHONPATH in the Python Console of QGIS with:
import os
os.getcwd()
and use the Control Panel of Windows to change it.
-
1I did try it (as noted in the original question). It doesn't work. Have you actually tried this from the
--code
option or only from the console? What version of QGIS are you running?Conley Owens– Conley Owens2015年08月24日 02:41:53 +00:00Commented Aug 24, 2015 at 2:41 -
Please, see my "Editing Note".xunilk– xunilk2015年08月24日 09:35:26 +00:00Commented Aug 24, 2015 at 9:35
-
1Thanks for the help, but I'm not sure what you mean by running the script in the bash console as
python myscript.py
, are you suggesting I use the api outside of qgis where I would then have to handle all the overhead of runningQgsApplication.initQgis()
and loading the project myself? If so, this is going an entirely different route.Conley Owens– Conley Owens2015年08月25日 17:06:24 +00:00Commented Aug 25, 2015 at 17:06
It works well for me to do this:
import os
os._exit(0)
It's also useful that you can set an exit code.
Another option in QGIS to exit the script and the QGIS Project is to write:
exit()
For instance:
else:
print("El usuario cancelo la entrada.")
exit()
So if the code runs the ELSE part, the exit will close the script and the project.
--screenshot
flag just doesn't cut it).sys.exit()
in my scripts which have worked.echo "sys.exit()" > code.py; qgis --code code.py
What version of QGIS were you running? How were you invoking the scripts?