3

I had created a file named "startup.py" in users python home directory (... .qgis2/python) and I had triggered the python console in startup.py file by

import qgis

qgis.utils.iface.actionShowPythonDialog().trigger()

That successfully triggers console as soon as QGIS opens. Now I want python scripts from local path to be loaded and executed in the console automatically using PyQGIS. How can this be done?

1

Add these lines to startup.py file.

from qgis.utils import iface

def test():
    print("Successful")

iface.actionShowPythonDialog().triggered.connect(test)
iface.actionShowPythonDialog().trigger()

enter image description here

OR

from qgis.utils import iface
from foo import bar

iface.actionShowPythonDialog().triggered.connect(bar)
iface.actionShowPythonDialog().trigger()

If you want to run the script while opening a project, add those lines to openProject method in "Project Properties > Macros". And then you should add the highlighted line in the script below. Otherwise, when every clicking the "Python Console" button, bar method runs.

def openProject():
    from qgis.utils import iface
    from foo import bar
    iface.actionShowPythonDialog().triggered.connect(bar)
    iface.actionShowPythonDialog().trigger()
    iface.actionShowPythonDialog().triggered.disconnect(bar)

foo.py:

def bar():
    print("Successful")

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.