使用ipdb调试一个单元格中的python代码(jupyter或Ipython) [英] using ipdb to debug python code in one cell (jupyter or Ipython)

查看:1328
本文介绍了使用ipdb调试一个单元格中的python代码(jupyter或Ipython)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有firefox的jupyter(或Ipython)笔记本电脑,并希望在单元格中调试一些python代码。我使用'import ipdb; ipdb.set_trace()'作为断点,例如我的单元格具有以下代码:

  a = 4 
import ipdb; ipdb.set_trace()
b = 5
打印a
打印b

其中使用Shift + Enter执行后会给我这个错误:

  ------------ -------------------------------------------------- ------------ 
MultipleInstanceError追溯(最近的最后一次调用)
< ipython-input-1-f2b356251c56>在< module>()
1 a = 4
----> 2 import ipdb; ipdb.set_trace()
3 b = 5
4打印a
5打印b

/home/nnn/anaconda/lib/python2.7/site- < module>()中的packages / ipdb / __ init__.py
14#您应该已经收到GNU通用公共许可证的副本以及此程序。如果没有,请参阅http://www.gnu.org/licenses/。
15
---> 16从ipdb .__ main__ import set_trace,post_mortem,pm,run,runcall,runeval,launch_ipdb_on_exception
17
18 pm#请pyflakes

/ home / nnn / anaconda / lib / < module>()中的python2.7 / site-packages / ipdb / __ main__.py
71#实例方法将创建一个新的而不加载配置。
72#即:如果我们在一个嵌入式实例中,我们不想加载配置。
---> 73 ipapp = TerminalIPythonApp.instance()
74 shell = get_ipython()
75 def_colors = shell.colors

/home/nnn/anaconda/lib/python2.7/site packages / traitlets / config / configured.pyc(cls,* args,** kwargs)
413 raise MultipleInstanceError(
414''
- >的多个不兼容的子类实例正在创建415'%s'%cls .__ name__
416)
417

MultipleInstanceError:正在创建TerminalIPythonApp的多个不兼容的子类实例。

如果我使用这个代码不在浏览器中的jupyter笔记本中,而在jupyter qtconsole。
这个错误是什么意思,要做什么来避免这个错误?
可以使用pdb调试器的next,continue,etc命令逐个调试单元格中的代码吗?

解决方案

有这个问题,它似乎与jupyter和ipdb的版本有关。



解决方案是使用这个而不是ipdb库 set_trace call:

 从IPython.core.debugger import Tracer 
示踪剂()()#this一个触发调试器

源:http://devmartin.com/blog/2014/10/trigger-ipdb-within-ipython-notebook/


I'm using jupyter (or Ipython) notebook with firefox, and want to debug some python code in the cell. I am using 'import ipdb; ipdb.set_trace()' as kind of breakpoint, for example my cell has the following code:

a=4
import ipdb; ipdb.set_trace()
b=5
print a
print b

which after execution with Shift+Enter gives me this error:

--------------------------------------------------------------------------
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.

The same error appears if I use this code not in the jupyter notebook in the browser, but in jupyter qtconsole. What does this error mean and what to do to avoid it? Is it possible to debug code in the cell step-by-step, using next, continue, etc commands of pdb debugger?

解决方案

Had this problem also and it seems to be related to versions of jupyter and ipdb.

Solution is to use this instead of the ipdb library set_trace call:

from IPython.core.debugger import Tracer
Tracer()() #this one triggers the debugger

Source: http://devmartin.com/blog/2014/10/trigger-ipdb-within-ipython-notebook/

这篇关于使用ipdb调试一个单元格中的python代码(jupyter或Ipython)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆