连接现有Jupyter Notebook时QtConsole启动报错求助
现象
- 可正常启动独立Jupyter QtConsole:
$ jupyter qtconsole - 使用以下命令连接现有Notebook内核时,会弹出无法使用的空白QtConsole并报错:
或指定内核文件:$ jupyter qtconsole --existing$ jupyter qtconsole --existing kernel-e73a6873-0080-4bd5-9433-3ekk5c52b5c3.json
报错信息
ERROR:tornado.general:Uncaught exception in ZMQStream callback Traceback (most recent call last): File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 556, in _run_callback callback(*args, **kwargs) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/jupyter_client/threaded.py", line 121, in _handle_recv msg_list = self.ioloop._asyncio_event_loop.run_until_complete(get_msg(future_msg)) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 623, in run_until_complete self._check_running() File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 585, in _check_running raise RuntimeError( RuntimeError: Cannot run the event loop while another loop is running ERROR:tornado.general:Uncaught exception in zmqstream callback Traceback (most recent call last): File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 577, in _handle_events self._handle_recv() File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 606, in _handle_recv self._run_callback(callback, msg) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 556, in _run_callback callback(*args, **kwargs) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/jupyter_client/threaded.py", line 121, in _handle_recv msg_list = self.ioloop._asyncio_event_loop.run_until_complete(get_msg(future_msg)) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 623, in run_until_complete self._check_running() File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 585, in _check_running raise RuntimeError( RuntimeError: Cannot run the event loop while another loop is running ERROR:asyncio:Exception in callback BaseAsyncIOLoop._handle_events(24, 1) handle: <Handle BaseAsyncIOLoop._handle_events(24, 1)> Traceback (most recent call last): File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/events.py", line 80, in _run self._context.run(self._callback, *self._args) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 189, in _handle_events handler_func(fileobj, events) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 577, in _handle_events self._handle_recv() File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 606, in _handle_recv self._run_callback(callback, msg) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 556, in _run_callback callback(*args, **kwargs) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/jupyter_client/threaded.py", line 121, in _handle_recv msg_list = self.ioloop._asyncio_event_loop.run_until_complete(get_msg(future_msg)) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 623, in run_until_complete self._check_running() File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 585, in _check_running raise RuntimeError( RuntimeError: Cannot run the event loop while another loop is running ERROR:tornado.general:Uncaught exception in ZMQStream callback Traceback (most recent call last): File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 556, in _run_callback callback(*args, **kwargs) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/jupyter_client/threaded.py", line 121, in _handle_recv msg_list = self.ioloop._asyncio_event_loop.run_until_complete(get_msg(future_msg)) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 623, in run_until_complete self._check_running() File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 585, in _check_running raise RuntimeError( RuntimeError: Cannot run the event loop while another loop is running ERROR:tornado.general:Uncaught exception in zmqstream callback Traceback (most recent call last): File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 577, in _handle_events self._handle_recv() File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 606, in _handle_recv self._run_callback(callback, msg) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 556, in _run_callback callback(*args, **kwargs) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/jupyter_client/threaded.py", line 121, in _handle_recv msg_list = self.ioloop._asyncio_event_loop.run_until_complete(get_msg(future_msg)) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 623, in run_until_complete self._check_running() File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 585, in _check_running raise RuntimeError( RuntimeError: Cannot run the event loop while another loop is running ERROR:asyncio:Exception in callback BaseAsyncIOLoop._handle_events(22, 1) handle: <Handle BaseAsyncIOLoop._handle_events(22, 1)> Traceback (most recent call last): File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/events.py", line 80, in _run self._context.run(self._callback, *self._args) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/tornado/platform/asyncio.py", line 189, in _handle_events handler_func(fileobj, events) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 577, in _handle_events self._handle_recv() File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 606, in _handle_recv self._run_callback(callback, msg) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/zmq/eventloop/zmqstream.py", line 556, in _run_callback callback(*args, **kwargs) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages/jupyter_client/threaded.py", line 121, in _handle_recv msg_list = self.ioloop._asyncio_event_loop.run_until_complete(get_msg(future_msg)) File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 623, in run_until_complete self._check_running() File "/home/bilgen/anaconda3/envs/ml_env/lib/python3.9/asyncio/base_events.py", line 585, in _check_running raise RuntimeError( RuntimeError: Cannot run the event loop while another loop is running /home/bilgen/anaconda3/envs/ml_env/lib/python3.9/functools.py:834: RuntimeWarning: coroutine 'get_msg' was never awaited impl = registry[cls] RuntimeWarning: Enable tracemalloc to get the object allocation traceback
环境信息
Ubuntu版本:
$ lsb_release -aNo LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.4 LTS
Release: 20.04
Codename: focalJupyter Notebook版本:
$ pip show notebookName: notebook
Version: 6.4.12
Summary: A web-based notebook environment for interactive computing
Home-page: http://jupyter.org
Author: Jupyter Development Team
Author-email: jupyter@googlegroups.com
License: BSD
Location: /home/bilgen/anaconda3/envs/ml_env/lib/python3.9/site-packages
Requires: argon2-cffi, ipykernel, ipython-genutils, jinja2, jupyter-client, jupyter-core, nbconvert, nbformat, nest-asyncio, prometheus-client, pyzmq, Send2Trash, terminado, tornado, traitlets
Required-by:QtConsole版本:
$ pip show qtconsoleName: qtconsole
Version: 5.3.1
Summary: Jupyter Qt console
Home-page: http://jupyter.org
Author: Jupyter Development Team
Author-email: jupyter@googlegroups.com
License: BSD
Requires: ipykernel, ipython-genutils, jupyter-client, jupyter core, pygments, pyzmq, qtpy, traitlets
Required-by:
解决方案
方案1:降级jupyter-client版本
该报错源于jupyter-client与QtConsole、Notebook的异步事件循环处理不兼容,尝试降级到7.4.9版本:
pip install jupyter-client==7.4.9
之后重新运行jupyter qtconsole --existing测试连接。
方案2:禁用nest-asyncio
Notebook默认启用nest-asyncio允许嵌套事件循环,会与QtConsole的事件循环冲突:
- 生成/打开Notebook配置文件:
jupyter notebook --generate-config - 在配置文件中添加或修改:
c.NotebookApp.use_nest_asyncio = False - 重启Notebook内核后再尝试连接。
方案3:临时替代方案
若上述方法无效,可使用ipython console连接现有内核:
ipython console --existing
内容的提问来源于stack exchange,提问作者ibilgen

