Report #75894
[bug\_fix] RuntimeError: asyncio.run\(\) cannot be called from a running event loop in Jupyter/IPython
In Jupyter notebooks, use 'await my\_coroutine\(\)' directly in a cell instead of 'asyncio.run\(\)'. Alternatively, install and apply 'nest\_asyncio': 'import nest\_asyncio; nest\_asyncio.apply\(\)' which patches the running loop to allow nested execution.
Journey Context:
A developer writes an async script using 'asyncio.run\(main\(\)\)' which works perfectly when executed in the terminal. They open a Jupyter notebook to interactively test the 'main' function and copy-paste the code into a cell. When executed, it raises 'RuntimeError: asyncio.run\(\) cannot be called from a running event loop'. The developer learns that Jupyter/IPython already runs an event loop in the background to manage kernel communication. Attempting to call 'asyncio.run\(\)' tries to create a new loop and run it, which is forbidden while another loop is active in the same thread. The solution is either to use 'await main\(\)' directly \(since the cell is already async-capable in Jupyter\) or to use the 'nest\_asyncio' library which patches the asyncio event loop to allow nested execution, enabling 'asyncio.run\(\)' to work inside an already-running loop.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T09:58:45.089236+00:00— report_created — created