2009-07-14 32 views

回答

104

有一個ipdb項目,該項目的IPython嵌入到標準的pdb,所以你可以這樣做:

import ipdb; ipdb.set_trace() 

這是安裝通過通常pip install ipdb

ipdb很短,所以不是easy_installing你也可以在Python路徑上創建文件ipdb.py地方和以下內容粘貼到文件中:

import sys 
from IPython.Debugger import Pdb 
from IPython.Shell import IPShell 
from IPython import ipapi 

shell = IPShell(argv=['']) 

def set_trace(): 
    ip = ipapi.get() 
    def_colors = ip.options.colors 
    Pdb(def_colors).set_trace(sys._getframe().f_back) 
+2

太棒了!這太酷了! – Geo 2009-07-14 18:22:02

+0

與Django的效果非常好。好吧,除了我看不到我輸入的文本,但這可能很容易修復(因爲ipdb只有六行)。 – 2010-01-24 12:50:38

+4

實際上,問題在於Django爲runserver分配了一個單獨的線程,並且在您進行代碼編輯時,它會重新生成線程。這通常工作正常,但如果你坐在pdb線程死亡,終端瘋了。你可以通過跳出runserver,runniny'stty sane'來解決這個問題,然後再次啓動runserver。 – 2011-09-12 20:17:13

8

通常,當我使用ipython的時候,我會用它裏面的「pdb」命令打開自動調試。

然後,在我的腳本所在的目錄中使用「run myscript.py」命令運行我的腳本。

如果我得到一個異常,ipython會停止調試器中的程序。退房的幫助下命令IPython的命令魔術(%魔法)

+0

所以沒有寫東西像ipython.set_trace的方式()? :) – Geo 2009-07-14 18:10:08

3

IPython docs

import IPython.ipapi 
namespace = dict(
    kissa = 15, 
    koira = 16) 
IPython.ipapi.launch_new_instance(namespace) 

將編程啓動一個IPython的殼。顯然,namespace字典中的值只是虛擬值 - 在實踐中使用locals()可能更有意義。

請注意,您必須對此進行硬編碼;它不會像pdb那樣工作。如果這就是你想要的,DoxaLogos的答案可能更像你正在尋找的東西。

11

import pdb; pdb.set_trace() 

等效與IPython是類似的:

from IPython.ipapi import make_session; make_session() 
from IPython.Debugger import Pdb; Pdb().set_trace() 

這有點冗長,但很高興知道你是否沒有安裝ipdb。呼叫需要一次來設置配色方案等,並且set_trace呼叫可以放置在任何需要中斷的地方。

3

的快速而簡便的方法:

from IPython.Debugger import Tracer 
debug = Tracer() 

然後只寫

debug() 

,無論你想開始調試程序。

+1

`ImportError:在python 3.4/IPython上沒有名爲'IPython.Debugger'的模塊3 – ostrokach 2015-05-31 15:33:37

47

在IPython中0。11,你可以在你的代碼中直接嵌入的IPython這樣

程序可能看起來像這樣

In [5]: cat > tmpf.py 
a = 1 

from IPython import embed 
embed() # drop into an IPython session. 
     # Any variables you define or modify here 
     # will not affect program execution 

c = 2 

^D 

這是當你運行它會發生什麼(我隨意選擇運行現有的IPython會話中。按照我的經驗嵌套ipython會話會導致它崩潰)。

In [6]: 

In [6]: run tmpf.py 
Python 2.7.2 (default, Aug 25 2011, 00:06:33) 
Type "copyright", "credits" or "license" for more information. 

IPython 0.11 -- An enhanced Interactive Python. 
?   -> Introduction and overview of IPython's features. 
%quickref -> Quick reference. 
help  -> Python's own help system. 
object? -> Details about 'object', use 'object??' for extra details. 

In [1]: who 
a  embed 

In [2]: a 
Out[2]: 1 

In [3]: 
Do you really want to exit ([y]/n)? y 


In [7]: who 
a  c  embed 
7

我想簡單地粘貼在我的腳本這一個班輪,我想設置一個斷點:

__import__('IPython').Debugger.Pdb(color_scheme='Linux').set_trace() 

較新的版本可以使用:

__import__('IPython').core.debugger.Pdb(color_scheme='Linux').set_trace() 
11

如果你使用更現代版本的IPython(> 0.10.2),您可以使用類似

from IPython.core.debugger import Pdb 
Pdb().set_trace() 

但是,它可能會更好地使用ipdb

6

看起來模塊最近已經混亂了一點。在IPython的0.13.1以下工作對我來說

from IPython.core.debugger import Tracer; breakpoint = Tracer() 

breakpoint() # <= wherever you want to set the breakpoint 

雖然很可惜,這一切都非常worthless in qtconsole

4

較新版本的IPython提供了一種將IPython會話嵌入和嵌套到任何Python程序的簡單機制。您可以按照the following recipe嵌入IPython的會話:

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解釋器。

2

我不得不穀歌這一對夫婦,如果時間過去幾天所以添加一個答案...有時很高興能夠正常運行腳本,只有放入ipython/ipdb的錯誤,而不必把ipdb.set_trace()斷點到代碼

ipython --pdb -c "%run path/to/my/script.py --with-args here" 

(第一pip install ipythonpip install ipdb當然)

相關問題