IPython refuses to update the namespace. fix #396

IPython embedding code borrowed from https://github.com/mitsuhiko/werkzeug/pull/85
This commit is contained in:
Daniel Graña 2013-10-10 00:56:52 -02:00
parent 7b1288ba54
commit aa6fb7daaa
1 changed files with 12 additions and 8 deletions

View File

@ -1,21 +1,25 @@
def start_python_console(namespace=None, noipython=False):
def start_python_console(namespace=None, noipython=False, banner=''):
"""Start Python console binded to the given namespace. If IPython is
available, an IPython console will be started instead, unless `noipython`
is True. Also, tab completion will be used on Unix systems.
"""
if namespace is None:
namespace = {}
try:
try: # use IPython if available
if noipython:
raise ImportError
import IPython
raise ImportError()
try:
IPython.embed(user_ns=namespace)
except AttributeError:
shell = IPython.Shell.IPShellEmbed(argv=[], user_ns=namespace)
shell()
from IPython.frontend.terminal.embed import InteractiveShellEmbed
sh = InteractiveShellEmbed(banner1=banner)
except ImportError:
from IPython.Shell import IPShellEmbed
sh = IPShellEmbed(banner=banner)
sh(global_ns={}, local_ns=namespace)
except ImportError:
import code
try: # readline module is only available on unix systems
@ -25,6 +29,6 @@ def start_python_console(namespace=None, noipython=False):
else:
import rlcompleter
readline.parse_and_bind("tab:complete")
code.interact(banner='', local=namespace)
code.interact(banner=banner, local=namespace)
except SystemExit: # raised when using exit() in python code.interact
pass