Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I would like to run a few small and simple python scripts for QGIS (Mac) from "outside" of QGIS. With outside I mean in this context, either the normal os command line (terminal.app) or even better, directly out of Sublime Text (text-editor), but definitely not via the inbuilt QGIS python console. I've read through various tutorial e.g. http://www.qgis.org/pyqgis-cookbook/intro.html#python-applications and I am able to get a reference to the QGIS app, but unfortunately not to qgis.utils.iface or something else deeper. This little code snippet should for instance print out the name of the active layer ... here is what I have:

    import sys

    from qgis.core import *
    import qgis.utils

    print "helo" # console output: helo

    QgsApplication.setPrefixPath("/Applications/QGIS.app/", True)

    print QgsApplication # console output: <class 'qgis.core.QgsApplication'>

    print qgis.utils.iface # = console output: none

    aLayer = qgis.utils.iface.activeLayer()
    print aLayer.name()


Please don't reply me now how to create a plugin or something else "heavy". I am just looking for a quick an easy way to shoot scripts out of a comfortable text-editor to QGIS. Many thanks!

share|improve this question
add comment

1 Answer

up vote 8 down vote accepted

You can't get a reference to the iface object here because it doesn't exist in this context. The iface (QgisInterface) object is a convenience object for plugins, or scripts running inside QGIS, to access the main objects e.g. the map canvas, legend, composer, etc, and only exists when the main application is running.

When you create a standalone Python script using the QGIS APIs none of this stuff exists because you are making your own mapping application.

There are three different situations:

  1. A QGIS plugin
  2. A script that runs inside QGIS (not a plugin) for automation
  3. Standalone app using QGIS APIs

1. and 2. have access to iface, the last one doesn't.

For 3 if you want to create a script that opens a layer in a map canvas you would do this after QgsApplication.initQgis()

map = QgsMapCanavs()
layer = QgsVectoryLayer('path.shp','myshapefile','ogr')

However if you are really looking for something like 2 then you can just write this in your script editor

from qgis.core import *
from qgis.gui import *
import qgis.utils


but this has to be run inside QGIS for qgis.utils to work. That can be done by putting the script on PATH and running import scriptname in the Python console or by using the ScriptRunner plugin.

Note the following isn't QGIS yet

There is a number 4 that hasn't been added yet, and hopefully will be in the future, and that is the option to run QGIS with a commandline arg to say run this code.

For example:

qgis --code=mycodefile.py

Plugin logging (version 1.8)

You can use the QgsMessageLog class to log information to the QGIS log window. The yellow exclamation mark in the bottom right corner.

from qgis.core import *
log = lambda m: QgsMessageLog.logMessage(m,'My Plugin') 
log('My message')

or without using lambda

QgsMessageLog.logMessage('My message', 'My Plugin')

I prefer the lambda based one as it is shorter and less typing any time you want to log something.

share|improve this answer
Thank you for clarification and the swift reply! then I am waiting desperately for option 4. In the meantime I have to live with 2, which is really a pain doing all the time import script followed by reload(script) and of course change from my editor to QGIS. I also checked the scriptrunner plugin, which is promising but then I was not able to get any print "debug value" to the QGIS console. Do you know, by any chance, a way how to have a scriptrunner script printing something to the QGIS console? Many thanks! Will wait for your reply and then close the question! –  user8880 Jul 16 '12 at 17:06
The python console is not really meant for that kind of output, more just for input. What you can do is log to the QGIS log window. Will add to the answer to explain this. –  Nathan W Jul 17 '12 at 2:06
thanks! that is extremely helpful. my workaround was to abuse a popup to have a debug output def info(text): QMessageBox.information(None,"Debug Info", str(text)) –  user8880 Jul 17 '12 at 12:48
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.