ipdb를 사용하여 하나의 셀 (jupyter 또는 Ipython)에서 Python 코드 디버그
firefox와 함께 jupyter (또는 Ipython) 노트북을 사용하고 있으며 셀에서 일부 Python 코드를 디버깅하고 싶습니다. 나는 'import ipdb; ipdb.set_trace () '를 일종의 중단 점으로 사용합니다. 예를 들어 내 셀에는 다음 코드가 있습니다.
a=4
import ipdb; ipdb.set_trace()
b=5
print a
print b
Shift + Enter로 실행 한 후이 오류가 발생합니다.
--------------------------------------------------------------------------
MultipleInstanceError Traceback (most recent call last)
<ipython-input-1-f2b356251c56> in <module>()
1 a=4
----> 2 import ipdb; ipdb.set_trace()
3 b=5
4 print a
5 print b
/home/nnn/anaconda/lib/python2.7/site-packages/ipdb/__init__.py in <module>()
14 # You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
15
---> 16 from ipdb.__main__ import set_trace, post_mortem, pm, run, runcall, runeval, launch_ipdb_on_exception
17
18 pm # please pyflakes
/home/nnn/anaconda/lib/python2.7/site-packages/ipdb/__main__.py in <module>()
71 # the instance method will create a new one without loading the config.
72 # i.e: if we are in an embed instance we do not want to load the config.
---> 73 ipapp = TerminalIPythonApp.instance()
74 shell = get_ipython()
75 def_colors = shell.colors
/home/nnn/anaconda/lib/python2.7/site-packages/traitlets/config/configurable.pyc in instance(cls, *args, **kwargs)
413 raise MultipleInstanceError(
414 'Multiple incompatible subclass instances of '
--> 415 '%s are being created.' % cls.__name__
416 )
417
MultipleInstanceError: Multiple incompatible subclass instances of TerminalIPythonApp are being created.
브라우저의 jupyter 노트북이 아니라 jupyter qtconsole에서이 코드를 사용하면 동일한 오류가 나타납니다. 이 오류는 무엇을 의미하며이를 방지하려면 어떻게해야합니까? pdb 디버거의 next, continue 등의 명령을 사용하여 단계별로 셀에서 코드를 디버깅 할 수 있습니까?
이 문제도 있었고 jupyter 및 ipdb의 버전과 관련된 것으로 보입니다.
해결책은 ipdb 라이브러리 set_trace
호출 대신 이것을 사용하는 것입니다 .
from IPython.core.debugger import Tracer
Tracer()() #this one triggers the debugger
출처 : http://devmartin.com/blog/2014/10/trigger-ipdb-within-ipython-notebook/
Jupyter Notebook을 사용하는 경우 매직 명령 " %% debug "로 셀을 시작합니다 . 그러면 디버깅 세션을 탐색하는 데 도움이되는 ipdb 행이 셀 맨 아래에 표시됩니다. 다음 명령을 사용하면 시작할 수 있습니다.
n- 현재 행을 실행하고 다음 행으로 이동합니다.
c- 다음 중단 점까지 실행을 계속합니다.
디버깅을 결정할 때마다 커널을 다시 시작하여 모든 변수가 새로 할당되도록하십시오. ipdb 줄을 통해 각 변수의 값을 확인할 수 있으며 할당 된 줄을 실행할 때까지 변수가 정의되지 않은 것을 확인할 수 있습니다. 그 변수에 값.
%%debug
import pdb
from pdb import set_trace as bp
def function_xyz():
print('before breakpoint')
bp() # This is a breakpoint.
print('after breakpoint')
My version of Jupyter is 5.0.0 and my corresponding ipython version is 6.1.0. I am using
import IPython.core.debugger
dbg = IPython.core.debugger.Pdb()
dbg.set_trace()
Tracer
is listed as deprecated.
Update:
I tried using the method from another answer https://stackoverflow.com/a/43086430/8019692 below but got an error:
MultipleInstanceError: Multiple incompatible subclass instances of TerminalIPythonApp are being created.
I prefer my method to the %%debug magic since I can set breakpoints in functions defined in other cells and run the function in another cell. Jupyter/IPython drops into the debugger in my function where the breakpoint is set, and I can use the usual pdb
commands. To each his own...
@lugger1, the accepted answer is deprecated.
Tracer()
is deprecated.
Use:
from IPython.core.debugger import set_trace
and then place set_trace()
where breakpoint is needed.
from IPython.core.debugger import set_trace
def add_to_life_universe_everything(x):
answer = 42
set_trace()
answer += x
return answer
add_to_life_universe_everything(12)
This works fine and brings us a little bit more comfort (e.g. syntax highlighting) than just using the built-in pdb.
'programing' 카테고리의 다른 글
namesched 모듈 인증에 중복된 이름 공간 auth/ (0) | 2022.07.02 |
---|---|
에넘이란 무엇이며 왜 유용한가? (0) | 2022.07.02 |
Python 스크립트 "예상 된 2D 배열, 대신 1D 배열이 있습니다."오류가 발생합니까? (0) | 2021.01.18 |
makefile에서 -I와 -L의 차이점은 무엇입니까? (0) | 2021.01.18 |
감사와 로깅의 차이점은 무엇입니까? (0) | 2021.01.18 |