Python代码在ipython中作为脚本和脚本运行,但在jupyter-notebook中引发错误

时间:2020-08-18 14:38:07

标签: python python-3.x jupyter-notebook ipython

我正在尝试在Jupyter笔记本中运行我的代码的片段,但失败了。但是,相同的代码段既作为脚本又作为python脚本运行。

代码:


import asyncio

async def a_square(x):
    print(f'Asynchronously squaring {x}!')
    return x ** 2


asyncio.run(a_square(2))

Jupyter错误

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-4-e2feaf9936d2> in <module>
      5     return x ** 2
      6 
----> 7 asyncio.run(a_square(2))

/usr/lib/python3.8/asyncio/runners.py in run(main, debug)
     31     """
     32     if events._get_running_loop() is not None:
---> 33         raise RuntimeError(
     34             "asyncio.run() cannot be called from a running event loop")
     35 

RuntimeError: asyncio.run() cannot be called from a running event loop

Python路径,所有版本均相同。以下语句为Jupyter,Ipython以及从脚本执行时提供了相同的输出。

import sys
print(sys.executable)
print(sys.version)

输出:

/usr/bin/python3
3.8.5 (default, Jul 27 2020, 08:42:51) 
[GCC 10.1.0]

0 个答案:

没有答案