Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use IPython in an embedded interactive Python console?

Tags:

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()
like image 399
Mat Avatar asked Jan 31 '09 22:01

Mat


People also ask

What is the difference between Python Console and IPython Console?

Compared to Python, IPython (created by Fernando Perez in 2001) can do every thing what python can do. Ipython provides even extra features like tab-completion, testing, debugging, system calls and many other features. You can think IPython as a powerful interface to the Python language.

Is IPython is an enhanced interactive Python shell?

The goal of IPython is to create a comprehensive environment for interactive and exploratory computing. To support this goal, IPython has three main components: An enhanced interactive Python shell.

Can you use IPython without Jupyter?

The IPython console is now deprecated and if you want to start it, you'll need to use the Jupyter Console, which is a terminal-based console frontend for Jupyter kernels.


2 Answers

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()
like image 187
Dereck Wonnacott Avatar answered Sep 21 '22 16:09

Dereck Wonnacott


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 
like image 33
f3lix Avatar answered Sep 20 '22 16:09

f3lix