2012-12-16 48 views
0

我已經添加在函數斷言(0),以瞭解的函數的順序則調用此函數。如何查看完整的追溯?

例:

def my_func(): 
    ...lines of code... 
    assert(0) 
    ...more lines of code... 

日誌只顯示這一點:

Traceback (most recent call last): 
    File "/var/www/folder/file.py", line 273, in my_func 
    assert(0) 
AssertionError 

我想看到完整的呼叫跟蹤 - 例如:first_func - > second_func - > my_func,並將

我試着回溯庫,但它顯示了我相同的堆棧跟蹤。 請讓我知道我在這裏失蹤。

+0

可能重複http://stackoverflow.com/questions/3702675/print-the-full-traceback-in-python-without-宿營的程序) – Nathan

+0

,而@阿列克謝的答案是有幫助的,如果你想** **完全回溯(即,你通常會看到在命令行),您可以使用來自一個班輪[我的答案]( http://stackoverflow.com/a/33723119/1467306)到一個類似的問題。 –

回答

3

使用traceback模塊這一點。即

>>> import traceback 
>>> def my_func(): 
...  my_other_func() 
... 
>>> def my_other_func(): 
...  my_third() 
... 
>>> def my_third(): 
...  print "Stack" 
...  traceback.print_stack() 
...  print "Extracted" 
...  print repr(traceback.extract_stack()) 
... 
>>> 
>>> my_func() 
Stack 
    File "<stdin>", line 1, in <module> 
    File "<stdin>", line 2, in my_func 
    File "<stdin>", line 2, in my_other_func 
    File "<stdin>", line 3, in my_third 
Extracted 
[('<stdin>', 1, '<module>', None), 
('<stdin>', 2, 'my_func', None), 
('<stdin>', 2, 'my_other_func', None), 
('<stdin>', 5, 'my_third', None)] 
>>> 
的[打印在python完整回溯(不停止程序)](
+0

感謝阿列克謝, traceback.extract_stack()幫助! – Jamal