Mat
Mat

Reputation: 86474

Can I use IPython in an embedded interactive Python console?

I use the following snippet to drop into a Python shell mid-program. This works fine, but I only get the standard console. Is there a way to do the same but using the IPython shell?

import code

class EmbeddedConsole(code.InteractiveConsole):
    def start(self):
        try:
                self.interact("Debug console starting...")
        except:
                print("Debug console closing...")

def print_names():
    print(adam)
    print(bob)

adam = "I am Adam"
bob = "I am Bob"

print_names()
console = EmbeddedConsole(locals())
console.start()
print_names()

Upvotes: 7

Views: 2823

Answers (2)

Dereck Wonnacott
Dereck Wonnacott

Reputation: 493

The answer by f3lix is no longer valid it seems, I was able to find this however:

At the top of your python script:

from IPython import embed

Wherever you want to spin up a console:

embed()

Upvotes: 15

f3lix
f3lix

Reputation: 29877

Embedding IPython might be interesting for you.

Mininum of code to run IPython in your app:

from IPython.Shell import IPShellEmbed
ipshell = IPShellEmbed()
ipshell() # this call anywhere in your program will start IPython 

Upvotes: 2

Related Questions