QGIS and IPython: the definitive interactive consoleWhatever is your level of Python knowledge, when you’ll discover the advantages and super-powers of IPython you will never run the default python console again, really: never! If you’ve never heard about IPython, discover it on IPython official website, don’t get confused by its notebook, graphics and parallel computing capabilities, it also worth if only used as a substitute for the standard Python shell. I discovered IPython more than 5 years ago and it literally changed my life: I use it also for debugging instead ofpdb, you can embed an IPython console in your code with:
from IPython import embed; embed()

TAB completion with full introspection

What I like the most in IPython is its TAB completion features, it’s not just like normal text matching while you type but it has full realtime introspection, you only see what you have access to, being it a method of an instance or a class or a property, a module, a submodule or whatever you might think of: it even works when you’re importing something or you are typing a path like in open('/home/..... Its TAB completion is so powerful that you can even use shell commands from within the IPython interpreter!

Full documentation is just a question mark away

Just type “?” after a method of function to print its docstring or its signature in case of SIP bindings.

Lot of special functions

IPython special functions are available for history, paste, run, include and many more topics, they are prefixed with “%” and self-documented in the shell.

All that sounds great! But what has to do with QGIS?

I personally find the QGIS python console lacks some important features, expecially with the autocompletion (autosuggest). What’s the purpose of having autocompletion when most of the times you just get a traceback because the method the autocompleter proposed you is that of another class? My brain is too small and too old to keep the whole API docs in my mind, autocompletion is useful when it’s intelligent enough to tell between methods and properties of the instance/class on which you’re operating. Another problem is that the API is very far from being “pythonic” (this isn’t anyone’s fault, it’s just how SIP works), here’s an example (suppose we want the SRID of the first layer):
core.QgsMapLayerRegistry.instance().mapLayers().value()[0].crs().authid()
# TAB completion stops working here^
TAB completion stop working at the first parenthesis 🙁 What if all those getter would be properties?
registry = core.QgsMapLayerRegistry.instance()
# With a couple of TABs without having to remember any method or function name!
registry.p_mapLayers.values()
[<qgis._core.QgsRasterLayer at 0x7f07dff8e2b0>,
 <qgis._core.QgsRasterLayer at 0x7f07dff8ef28>,
 <qgis._core.QgsVectorLayer at 0x7f07dff48c30>,
 <qgis._core.QgsVectorLayer at 0x7f07dff8e478>,
 <qgis._core.QgsVectorLayer at 0x7f07dff489d0>,
 <qgis._core.QgsVectorLayer at 0x7f07dff48770>]

layer = registry.p_mapLayers.values()[0]

layer.p_c ---> TAB!
layer.p_cacheImage            layer.p_children       layer.p_connect       
layer.p_capabilitiesString    layer.p_commitChanges  layer.p_crs           
layer.p_changeAttributeValue  layer.p_commitErrors   layer.p_customProperty

layer.p_crs.p_ ---> TAB!
layer.p_crs.p_authid               layer.p_crs.p_postgisSrid      
layer.p_crs.p_axisInverted         layer.p_crs.p_projectionAcronym
layer.p_crs.p_description          layer.p_crs.p_recentProjections
layer.p_crs.p_ellipsoidAcronym     layer.p_crs.p_srsid            
layer.p_crs.p_findMatchingProj     layer.p_crs.p_syncDb           
layer.p_crs.p_geographicCRSAuthId  layer.p_crs.p_toProj4          
layer.p_crs.p_geographicFlag       layer.p_crs.p_toWkt            
layer.p_crs.p_isValid              layer.p_crs.p_validationHint   
layer.p_crs.p_mapUnits    

layer.p_crs.p_authid
Out[]: u'EPSG:4326'

This works with a quick and dirty hack: propertize that adds a p_... property to all methods in a module or in a class that
  1. do return something
  2. do not take any argument (except self)
this leaves the original methods untouched (in case they were overloaded!) still allowing full introspection and TAB completion with a pythonic interface.
A few methods are still not working with propertize, so far singleton methods like instance() are not passing unit tests.

IPyConsole: a QGIS IPython plugin

If you’ve been reading up to this point you probably can’t wait to start using IPython inside your beloved QGIS (if that’s not the case, please keep reading the previous paragraphs carefully until your appetite is grown!). An experimental plugin that brings the magic of IPython to QGIS is now available:
Download IPyConsole   Please start exploring QGIS objects and classes and give me some feedback!   IPyConsole QGIS plugin

Installation notes

You basically need only a working IPython installation, IPython is available for all major platforms and distributions, please refer to the official documentation.  

7 Responses to “QGIS and IPython: the definitive interactive console”

  • Gary Sherman

    “No offense, but QGIS python console just sucks.”

    How can that statement not be offensive? Rather than denigrate work others have done, why not extol the virtues of your work?

  • Alessandro Pasotti

    @Gary,

    Sorry, I really didn’t want to offense anyone, I deeply respect the works of other developers, maybe my lack of knowledge of the english language made me write a sentence that was more harsh than I wanted.

    Thanks for pointing this out, I’ll rewrite it to be more soft.

  • Yves Moisan

    Note to users (Ubuntu 14.04) :

    Do not install ipython with apt; you’ll get Ipython 2.x and the plugin requires >=3.10

    Do not “pip install” without specifying a version because then you’ll get the most recent version of Ipython (4.x) that also brings about an error when hitting the “IPy” button in QGIS. Rather, install specifically 3.1.0 (didn’t try 3.2.x) :

    pip install ipython==3.1.0
    pip install qtconsole

    Cheers,

    Yves

  • Yves Moisan

    Forgot the obvious in my previous comment : thank you for this plugin …

    I hope I can use it to debug Python plugins.

  • Fabian

    Dear Qgis-Ipython,

    The installation though qgis 2.10 pisa, python 2.7.5 and ipython 3.1 is corrupted. I can´t make it work, can you state clearly which are the dependencies for your plugin or some installation stuff using pip?.

    Thanks in advance.

  • Alessandro Pasotti

    @Fabian,

    I’m very sorry it’s not working for you, all I can state clearly is that the plugin is actually working for me with
    Python 2.7.6
    IPython 3.1.0
    QGIS 2.12.0, it was also working with Pisa and it’s also working in 2.8.3 and in master (I have all the three version installed).