debugging - 使用 ipdb 在一个单元中调试 python 代码(jupyter 或 Ipython)

标签 debugging cell jupyter ipdb

我正在使用带有 firefox 的 jupyter(或 Ipython)笔记本,并且想要在单元中调试一些 python 代码。我正在使用“导入 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/

带注释的屏幕截图: screenshot illustrate how Tracer()() will cause Jupyter notebook to react. It pauses execution on the line in your code where you Trace()() and a new "inline" input accepts ipdb commands like 'p' or 'n' or 'c', shown here

关于debugging - 使用 ipdb 在一个单元中调试 python 代码(jupyter 或 Ipython),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35613249/

相关文章:

visual-studio - 无法设置下一个语句。下一条此时无法更改

uitableview - 以编程方式设置静态单元格的 detailtextlabel

java - 将鼠标悬停在另一个单元格上时如何删除 JTable 单元格 bgColor?

objective-c - 更改 UITableViewCell 框架但没有用

jupyter-notebook - 启动器中的 fatal error : Unable to create process using '"'

python - 无法在浏览器上打开 jupyter(ipython) notebook

c++ - Windows 调试 Api - 分离不起作用

android - Android OS调用的方法来检索内存?如何模拟呢?

pandas - Jupyter 中数据帧的漂亮输出

javascript - 关于这个 javascript 代码有什么问题有什么想法吗?