I would like to run a few small and simple Python scripts for QGIS (Mac) from "outside" of QGIS (e.g. Sublime Text). 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.


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.

  • 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! 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)) Jul 17 '12 at 12:48
  • typo: QgsVector_y_Layer Oct 7 '16 at 7:16

I think Nathan W's answer is out of date. I was able to run QGIS (version 2.6) python scripts from the command line (Nathan's option 4) using the following commands.

man qgis
qgis -nologo --project /path/foo.qgs --code /path/foo.py
  • 2
    This works but only after I close the "tips" window... is there a way to disable the interface totally and just execute the --code python script?
    – Anthony O.
    Jul 10 '18 at 13:17

Update for Nathan's option 4: (Windows, QGIS 2.18 Las Palmas)

To print QGIS help document,

qgis --help

To open QGIS, load a project, then, run a python script.

qgis --nologo --project c:/path/to/projfile.qgs --code c:/path/to/code.py

These commands should run on OSGeo4W Shell without problems.


QGIS 3.18 also works. Always use this command to see all the available options and the syntax of them:-

qgis --help

  • 3
    My answer is still valid for QGIS 3.8 (Zanzibar).
    – swatchai
    Oct 8 '19 at 15:39

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.