11

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.

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Aug 23, 2015 at 4:17
4
  • 1
    You can but why? Commented Aug 23, 2015 at 4:37
  • 1
    I'm automatically making a pdf with a print composer (the --screenshot flag just doesn't cut it). Commented Aug 23, 2015 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. Commented Aug 24, 2015 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? Commented Aug 25, 2015 at 17:09

3 Answers 3

8

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:

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
os.getcwd()

and use the Control Panel of Windows to change it.

answered Aug 23, 2015 at 10:44
3
  • 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? Commented Aug 24, 2015 at 2:41
  • Please, see my "Editing Note". Commented Aug 24, 2015 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. Commented Aug 25, 2015 at 17:06
2

It works well for me to do this:

import os
os._exit(0)

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

answered Nov 25, 2018 at 10:11
1

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.

answered Mar 6 at 11: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.