Report #62013
[bug\_fix] RuntimeError: This event loop is already running in Jupyter/IPython
Install and apply \`nest\_asyncio\`: \`import nest\_asyncio; nest\_asyncio.apply\(\)\` at the start of the notebook. This patches the asyncio policy to allow nested event loops, necessary because Jupyter already runs an asyncio loop for kernel communication.
Journey Context:
You open a Jupyter notebook and paste an async function from a web scraping script. You call \`asyncio.run\(fetch\_data\(\)\)\` in a cell. It raises \`RuntimeError: This event loop is already running\`. You try replacing it with \`await fetch\_data\(\)\` which works in the top-level cell, but then you refactor the code into a class method and \`await\` is invalid syntax there without an async def wrapper. You try \`loop = asyncio.get\_event\_loop\(\); loop.run\_until\_complete\(fetch\_data\(\)\)\` but get the same error. You research and discover that IPython/Jupyter kernels run a persistent asyncio event loop in the background to handle ZeroMQ messaging between the frontend and kernel. When you call \`asyncio.run\(\)\`, it checks \`asyncio.\_get\_running\_loop\(\)\`, finds Jupyter's loop, and crashes. You install \`nest\_asyncio\` via pip, import it at the top of your notebook, and run \`nest\_asyncio.apply\(\)\`. This patches the event loop to allow nested execution, letting \`asyncio.run\(\)\` reuse the existing loop instead of conflicting with it.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T10:34:28.982411+00:00— report_created — created