首页 > 解决方案 > 试图设置 ipdb.set_trace(): RuntimeError: There is no current event loop in thread 'Thread-....'

问题描述

我在 Windows 7 上使用 Miniconda 3,试图ipdb在我的程序中启动调试过程。我已经安装了 IPython ipdb,当然还有(在 virtualenv 中)。

测试程序:

#!/usr/bin/env python

import ipdb

def a():
    for i in range(10):
        print(i)
    ipdb.set_trace()
    print(100)

a()

我得到的是 Python 开始进入无限循环,一遍又一遍地喷出这样的异常:

Exception in thread Thread-3398:
Traceback (most recent call last):
  File "C:\ACME\Dev\Miniconda3\lib\threading.py", line 926, in _bootstrap_inner
    self.run()
  File "C:\ACME\Dev\Miniconda3\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\ACME\Dev\projects\example\venv\lib\site-packages\IPython\terminal\debugger.py", line 102, in in_thread
    line = self.pt_app.prompt()
  File "C:\ACME\Dev\projects\example\venv\lib\site-packages\prompt_toolkit\shortcuts\prompt.py", line 992, in prompt
    return get_event_loop().run_until_complete(self._dumb_prompt(self.message))
  File "C:\ACME\Dev\Miniconda3\lib\asyncio\events.py", line 644, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-3398'.

这可能是什么原因以及如何解决这个问题?

版本:

Python 3.7.6

ipdb==0.13.3
ipykernel==5.3.0
ipython==7.15.0
ipython-genutils==0.2.0
ipywidgets==7.5.1

(我列出了我发现的与 IPython 相关的所有软件包,如果重要的话,我还安装了 Jupyter)。

标签: pythonpython-3.xipythonpython-3.7ipdb

解决方案


我用新的代码片段替换了旧样式

import ipdb;
ipdb.set_trace() 

例如:

from IPython.core import debugger
debug = debugger.Pdb().set_trace

def buggy_method():
    debug()

我解决了这个问题。请参阅https://github.com/ipython/ipython/pull/9731/上的参考资料


推荐阅读