Programming

iPython 노트북에서 올바른 디버그 방법은 무엇입니까?

procodes 2020. 8. 5. 21:25
반응형

iPython 노트북에서 올바른 디버그 방법은 무엇입니까?


아시다시피 %debug magic한 셀 내에서 디버그 할 수 있습니다.

그러나 여러 셀에서 함수 호출이 있습니다.

예를 들어

In[1]: def fun1(a)
           def fun2(b)
               # I want to set a breakpoint for the following line #
               return do_some_thing_about(b)

       return fun2(a)

In[2]: import multiprocessing as mp
       pool=mp.Pool(processes=2)
       results=pool.map(fun1, 1.0)
       pool.close()
       pool.join

내가 시도한 것 :

  1. %debugcell-1의 첫 번째 줄에 설정하려고했습니다 . 그러나 cell-2를 실행하기 전에 즉시 디버그 모드로 들어갑니다.

  2. %debug코드 바로 앞에 줄 을 추가하려고 했습니다 return do_some_thing_about(b). 그러나 코드는 영원히 실행되며 멈추지 않습니다.

ipython 노트북 내에서 중단 점을 설정하는 올바른 방법은 무엇입니까?


ipdb 사용

통해 설치

pip install ipdb

용법:

In[1]: def fun1(a):
   def fun2(a):
       import ipdb; ipdb.set_trace() # debugging starts here
       return do_some_thing_about(b)
   return fun2(a)
In[2]: fun1(1)

라인 단위로 실행 n하고 함수로 단계적으로 사용 s하고 디버깅 프롬프트 사용을 종료하려면 c.

사용 가능한 명령의 전체 목록을 보려면 https://appletree.or.kr/quick_reference_cards/Python/Python%20Debugger%20Cheatsheet.pdf


ipdbjupyter 내부에서 다음을 사용할 수 있습니다 .

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


편집 : 위의 기능은 IPython 5.1부터 사용되지 않습니다. 이것이 새로운 접근법입니다.

from IPython.core.debugger import set_trace

set_trace()중단 점이 필요한 곳에 추가하십시오 . 입력 help에 대한 ipdb입력 필드가 나타나면 명령.


반환 기능은 def 기능 (주 기능)에 따라 하나의 탭을 제공해야합니다. 그리고 사용

%%debug 

대신에

%debug 

전체 셀뿐만 아니라 라인을 디버그합니다. 희망, 아마도 이것이 당신을 도울 것입니다.


언제든지 셀에 추가 할 수 있습니다.

import pdb; pdb.set_trace()

디버거가 해당 라인에서 중지됩니다. 예를 들면 다음과 같습니다.

In[1]: def fun1(a):
           def fun2(a):
               import pdb; pdb.set_trace() # debugging starts here
           return fun2(a)

In[2]: fun1(1)

그냥 입력 import pdbjupyter 노트북에, 다음이 사용 쪽지를 디버그. 매우 편리합니다.

c --> continue, s --> step, b 12 --> set break point at line 12 and so on.

Some useful links: Python Official Document on pdb, Python pdb debugger examples for better understanding how to use the debugger commands.

Some useful screenshots: enter image description hereenter image description here


The %pdb magic command is good to use as well. Just say %pdb on and subsequently the pdb debugger will run on all exceptions, no matter how deep in the call stack. Very handy.

If you have a particular line that you want to debug, just raise an exception there (often you already are!) or use the %debug magic command that other folks have been suggesting.


In Python 3.7 you can use breakpoint() function. Just enter

breakpoint()

wherever you would like runtime to stop and from there you can use the same pdb commands (r, c, n, ...) or evaluate your variables.


After you get an error, in the next cell just run %debug and that's it.


I just discovered PixieDebugger. Even thought I have not yet had the time to test it, it really seems the most similar way to debug the way we're used in ipython with ipdb

enter image description here

It also has an "evaluate" tab

참고URL : https://stackoverflow.com/questions/32409629/what-is-the-right-way-to-debug-in-ipython-notebook

반응형