我可以在嵌入式交互式Python控制台中使用IPython吗?

时间:2021-06-01 20:44:29

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?

我使用以下代码片段放入Python shell中间程序。这很好,但我只得到标准控制台。有没有办法做同样但使用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()

2 个解决方案

#1


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

f3lix的答案似乎不再有效,但我能够找到这个:

At the top of your python script:

在python脚本的顶部:

from IPython import embed

Wherever you want to spin up a console:

无论你想在哪里启动控制台:

embed()

#2


Embedding IPython might be interesting for you.

嵌入IPython可能对您有意义。

Mininum of code to run IPython in your app:

在您的应用中运行IPython的最小代码:

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

#1


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

f3lix的答案似乎不再有效,但我能够找到这个:

At the top of your python script:

在python脚本的顶部:

from IPython import embed

Wherever you want to spin up a console:

无论你想在哪里启动控制台:

embed()

#2


Embedding IPython might be interesting for you.

嵌入IPython可能对您有意义。

Mininum of code to run IPython in your app:

在您的应用中运行IPython的最小代码:

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