Last active
January 1, 2022 16:41
-
-
Save ducin/6882621 to your computer and use it in GitHub Desktop.
Python script opening interactive console with current interpreter state. Thanks to readline/rlcompleter, you may use up/down arrows (history browse) or left/right arrows (line edition), see http://stackoverflow.com/questions/19754458/python-open-interactive-console-from-script)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Console module provide `copen` method for opening interactive python shell in | |
the runtime. | |
""" | |
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) | |
shell.interact() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import console | |
example_list = [1, 2, 3] | |
example_tuple = ('abc', 'def') | |
console.copen(globals(), locals()) | |
print 'this will be continued' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After cloning this gist, run
in the console and type
dir()
to see that all modules, variables etc. are available