2009-01-31 45 views
7

我使用下面的代碼片段放入一個Python shell中程序。這工作正常,但我只得到標準控制檯。有沒有辦法做到這一點,但使用IPython外殼?我可以在嵌入式交互式Python控制檯中使用IPython嗎?

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() 
+0

請問您現在可以標記Dereck的答案嗎? – 2015-10-06 01:36:21

回答

13

通過f3lix答案不再有效看來,我能找到然而這樣的:

在你的Python的頂部腳本:

from IPython import embed 

不管你想要去旋轉了一個控制檯:

embed() 
2

Embedding IPython可能對您有興趣。

的最小值的代碼在你的應用程序運行的IPython:

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

請看下面,現在有一個來自IPython的嵌入函數可以用來代替。 – 2014-04-09 00:46:38