Skip to content

Instantly share code, notes, and snippets.

@jadient
Created March 8, 2015 21:32
Show Gist options
  • Save jadient/e00605b331814b3fea9a to your computer and use it in GitHub Desktop.
Save jadient/e00605b331814b3fea9a to your computer and use it in GitHub Desktop.
python open interactive console (for debugging) from Tomasz Ducin
"""
Console module provide `copen` method for opening interactive python shell in
the runtime.
http://sys-exit.blogspot.com/2013/12/python-open-interactive-console.html
Usage:
import console
console.copen(globals(), locals())
"""
import code
import readline
import rlcompleter
def copen(_globals, _locals):
"""
Opens interactive console with current execution state.
Call it with: `console.open(globals(), locals())`
"""
context = _globals.copy()
context.update(_locals)
readline.set_completer(rlcompleter.Completer(context).complete)
readline.parse_and_bind("tab: complete")
shell = code.InteractiveConsole(context)
print "Entering console, press Ctrl-D to exit"
shell.interact()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment