3

I'm working with QGIS 2.8 and windows 10. Is it possible that after open QGIS using the schedule tasks from windows 10, I could open the python console and run a script within QGIS programmatically. How can I automate the following actions after open the QGIS:

  1. open python console
  2. open a script
  3. run a script

Any help?


EDIT:

I've tried the two ways to open the python console, as suggested by Germán in this post "How to show Python console at QGIS program start" and it works. The problem occurs when I want to run my script. I tried to add my code these code lines as you suggest to define a startup.py file in my directory (C:\Users\USER\.qgis2\python), but as soon as I open QGIS I get this error:

"Traceback (most recent call last): File "", line 1, in File "C:/PROGRA~1/QGISWI~1/apps/qgis-ltr/./python\qgis\utils.py", line 478, in _import mod = _builtin_import(name, globals, locals, fromlist, level) File "C:/PROGRA~1/QGISWI~1/apps/qgis-ltr/./python\qgis\user.py", line 38, in execfile(startuppy, locals(), globals()) File "C:/Users/USER/.qgis2//python\startup.py", line 17, in layers =iface.legendInterface().layers() NameError: name 'iface' is not defined"
  • Regarding your edit, please see the new approach I suggest you in my answer. BTW, to solve the error you're getting, you'd need to import iface this way: from qgis.utils import iface. – Germán Carrillo May 19 '16 at 21:00
  • Thank's Germán, my script it's working! But just one more question, if I wanted to run another script in a sequence from the 1st one, how should I do? Imagine my 1st script's name:"myScript1.py" and the 2nd:"myScript2.py", should I write in Python macrosbox 'import myScript2.py' in the below line from 'import myScript1.py', and 'myScript2.run()' below 'def openProject():' line and 'myScript1.run()'? Should I move too "myScript2.py" to the same directory of "myScript1.py"? I tried to do this but just run the 1st script: "myScript1.py". What could I'm doing wrong? Have you got any idea? Thanks. – Patrícia Fernandes May 20 '16 at 10:04
  • Dear Patricia, please always stick to a simple and clear question from the beginning. Don't start to extend it, since that's tedious for people attempting to answer your original question. I suggest you to create another question where you make clear what scripts are you trying to run. I would honestly do what you just wrote, import myScript1, myScript2 and then in the openProject() function, myScript1.run() and myScript2.run(). If that's not running, explain what you get (any error message?) and what do you expect to get from your scripts. Regards. – Germán Carrillo May 20 '16 at 12:24
4

To automatically open the QGIS Python Console when QGIS starts, you can read How to show Python console at QGIS program start.

However, I guess your ultimate intention is to run the script (even if it doesn't run from the QGIS Python Console), so you can create (if it doesn't exist already) a startup.py file in the QGIS Python directory:

  • On GNU/Linux: /home/USER/.qgis2/python/
  • On Windows: C:\Program Files\QGIS\python\ or C:\Documents and Settings\$USERNAME\.qgis\python\ (I don't really know about paths on Windows 10, but if you have problems finding the correct Python path, just open the Plugin manager, click on any installed 3rd party plugin and read the path where it's installed).

And then either:

  1. Call your script from startup.py:

    import my_script     
    my_script.run() # If that's the method you want to execute from your script
    

    or,

  2. Just put your script code into the startup.py file, which is the same as copying your Python script to the aforementioned folder and rename it to startup.py


EDIT: Explain caveat when using the startup.py approach and suggest another approach

startup.py will be executed everytime you open QGIS, which gives you no control on whether you want to automatically run your script or just run QGIS to work on a new project.

Therefore, I suggest you to take another approach: use QGIS project macros.

Say you have a script (myScript.py) you want to run when QGIS starts from your schedule tasks. Copy/move the script into the QGIS Python path or into the same directory where your QGIS project will be saved.

Let's assume your script looks like this:

from qgis.utils import iface

def run():
    iface.addVectorLayer("/Path/To/geodata/myShape.shp","layer1","ogr")

Open a new QGIS project and click Project-->Project Properties...-->Macros, enable the Python macros checkbox, and write this:

import myScript

def openProject():
    myScript.run()

def saveProject():
    pass

def closeProject():
    pass

Click OK and go to Settings-->Options...-->General to choose Enable macros Always.

Save your QGIS project as, let's say, run_script_project.qgs.

Now, every time you want to get your script automatically executed when starting QGIS, just start QGIS from the console, in this way:

qgis --project /Path/To/Your/Project/run_script_project.qgs

Once QGIS opens the project, the run() method of your script will be executed.

  • Hi, Germán thank you for the idea. I've tried the two ways to open the python console like you suggest in this post "How to show Python console at QGIS program start". The problem occured when I want to run my script. I tried to add my code following these code lines as you suggest to define a startup.py file in my directory (C:\Users\USER\.qgis2\python): import qgis qgis.utils.iface.actionShowPythonDialog().trigger() – Patrícia Fernandes May 19 '16 at 11:13
  • How your script looks like? What are you trying to run? Do you expect QGIS to have some layers by default or are you loading them from your script? – Germán Carrillo May 19 '16 at 12:04

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.