How to embed an IPython interpreter in an application running in the IPython Qt console

There are several topics, but none of them are satisfactory.

I have a python application running in ipython qt console

http://ipython.org/ipython-doc/dev/interactive/qtconsole.html

When I encounter an error, I would like to be able to interact with the code at this point.

    try: 
      raise Exception()
    except Exception as e:
        try: # use exception trick to pick up the current frame
            raise None
        except:
            frame = sys.exc_info()[2].tb_frame.f_back
        namespace = frame.f_globals.copy()
        namespace.update(frame.f_locals)
        import IPython
        IPython.embed_kernel(local_ns=namespace)  

I would think this would work, but I get an error message:

RuntimeError: threads can only be run once

+5
source share
2 answers

I just use this:

from IPython import embed; embed()

works better than anything else for me :)

+24
source

You can follow the following recipe to inject an IPython session into your program:

try:
    get_ipython
except NameError:
    banner=exit_msg=''
else:
    banner = '*** Nested interpreter ***'
    exit_msg = '*** Back in main IPython ***'

# First import the embed function
from IPython.frontend.terminal.embed import InteractiveShellEmbed
# Now create the IPython shell instance. Put ipshell() anywhere in your code
# where you want it to open.
ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg)

ipshell() , IPython. ( ) IPython .

+4

All Articles