[IPython-User] A robust way to grab the IPython shell reference within a script / module

Ludwig Schwardt ludwig.schwardt@gmail....
Thu Mar 15 04:12:42 CDT 2012


Hi,

Simon Ratcliffe and I stumbled upon what may be the shortest, most robust and backwards compatible way to obtain a reference to the IPython shell from within a script or module (used e.g. to add custom tab completers for our objects):

try:
    # IPython 0.11 and above
    ip = get_ipython()
except NameError:
    # IPython 0.10 and below (or normal Python shell)
    ip = __builtins__.get('__IPYTHON__')
if ip is not None:
    # We are within IPython, so set up those completers
    ip.set_hook('complete_command', …)


This seems to work from at least 0.8.4 all the way to 0.12 (and beyond, I hope). Any comments, scorn or ridicule ("__builtins__? Are you nuts?") are very welcome.

Ludwig

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-user/attachments/20120315/49277607/attachment-0001.html 


More information about the IPython-User mailing list