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

Update: 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:

  Traceback (most recent call last):
    File "", line 1, in 
    File "myscript.py", line 14, in 

Update: os.kill(os.getpid(), 9) works but it's a dirty hack and I'm looking for something better.

  • 1
    You can but why? – Nathan W Aug 23 '15 at 4:37
  • 1
    I'm automatically making a pdf with a print composer (the --screenshot flag just doesn't cut it). – Conley Owens Aug 23 '15 at 4:39
  • Could you include your code into the question please as this could help potential answerers? I ask this as I also used sys.exit() in my scripts which have worked. – Joseph Aug 24 '15 at 9:55
  • echo "sys.exit()" > code.py; qgis --code code.py What version of QGIS were you running? How were you invoking the scripts? – Conley Owens Aug 25 '15 at 17:09

Try in your script:

from qgis.utils import iface

#your code here


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

print "Hello"


I ran qgis --code myscript.py at the bash console, inside the folder of the script, and immediately I got this error message:

enter image description here

and indicating that from qgis.utils import iface line is necessary.

When the first line is not commented the script execution had not errors:

enter image description here

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

and use the Control Panel of Windows to change it.

  • 1
    I 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 Aug 24 '15 at 2:41
  • Please, see my "Editing Note". – xunilk Aug 24 '15 at 9:35
  • 1
    Thanks 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 running QgsApplication.initQgis() and loading the project myself? If so, this is going an entirely different route. – Conley Owens Aug 25 '15 at 17:06

It works well for me to do this:

import os

It's also useful that you can set an exit code.

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.