Report #9486
[bug\_fix] RuntimeError: This event loop is already running \(in Jupyter/IPython\)
In Jupyter or IPython, replace 'asyncio.run\(coro\(\)\)' with 'await coro\(\)' directly in the cell. If you need to run asyncio code that uses 'asyncio.run' inside a library, install and apply 'nest\_asyncio' by running 'import nest\_asyncio; nest\_asyncio.apply\(\)' at the start of the notebook.
Journey Context:
You develop an async web scraper locally using 'asyncio.run\(main\(\)\)' which executes perfectly in a .py file. You then transition to a Jupyter Notebook to analyze the scraped data interactively. When you paste your 'asyncio.run\(main\(\)\)' code into a cell and execute it, you get RuntimeError: This event loop is already running. You check 'asyncio.get\_event\_loop\(\)' and confirm that the IPython kernel is already managing an event loop in the main thread. You try to refactor to use 'loop.create\_task\(\)' but struggle with gathering results. Searching reveals that 'asyncio.run\(\)' explicitly prohibits being called from a running loop. The solution is either to simply use 'await main\(\)' directly in the notebook cell \(since IPython supports top-level await\), or to import 'nest\_asyncio' which patches the asyncio loop to allow nested execution, enabling the original 'asyncio.run' code to work unchanged inside the notebook.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T08:17:27.571265+00:00— report_created — created