Install & Compatibility
Where this runs
tested against v1.7.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.210s · 17.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.184s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
apply
✓ import nest_asyncio2
nest_asyncio2.apply()
✗ import nest_asyncio
nest_asyncio.apply()
nest-asyncio2 is a direct replacement for the original `nest_asyncio` for Python 3.12+ compatibility. Ensure you use the correct package name.
This example demonstrates how to use `nest_asyncio2.apply()` to enable nested `asyncio.run()` calls. Without the patch, the inner `asyncio.run()` call would raise a `RuntimeError` because an event loop is already running.
import asyncio
import nest_asyncio2
async def inner_coroutine():
print("Inner coroutine running.")
await asyncio.sleep(0.01)
print("Inner coroutine finished.")
async def outer_coroutine():
print("Outer coroutine starting inner loop.")
# This would normally fail with RuntimeError: This event loop is already running
# but nest_asyncio2 allows it.
await asyncio.run(inner_coroutine())
print("Outer coroutine finished inner loop.")
# Apply the patch to allow nested event loops
nest_asyncio2.apply()
# Run the outer coroutine, which itself runs an inner event loop
print("Running outer event loop.")
asyncio.run(outer_coroutine())
print("Program finished.")
Debug
Known issues
breakingThe original `nest_asyncio` library is unmaintained and incompatible with Python 3.12+ (e.g., `asyncio.current_task()` and `loop_factory` issues). `nest-asyncio2` is a fork specifically addressing these incompatibilities.fixMigrate your `nest_asyncio` usage to `nest-asyncio2` by changing `import nest_asyncio` to `import nest_asyncio2` and updating `pip install` commands.
affects: Python 3.12+ when using `nest_asyncio`
gotcha`nest-asyncio2.apply()` will warn if `asyncio` is already patched by `nest_asyncio` (on Python 3.12+) or can be configured to raise a `RuntimeError` by setting `error_on_mispatched=True`.fixEnsure that only one of `nest_asyncio` or `nest-asyncio2` is applied. It's recommended to standardize on `nest-asyncio2` for modern Python versions.
affects: nest-asyncio2 v1.7.1+ on Python 3.12+
gotchaOn Python 3.12+, patched `asyncio.run()` might create a new event loop if none exists, which could lead to a `ResourceWarning: unclosed event loop` at exit. This warning is often hidden by default.fixThis is usually a cosmetic warning and doesn't require action. Be aware it might appear in specific environments or with custom warning filters.
affects: nest-asyncio2 on Python 3.12+
gotchaIn versions prior to `v1.7.2`, `asyncio.call_soon()` callbacks might not have been properly called during `asyncio.run()` in nested contexts.fixUpgrade to `nest-asyncio2` v1.7.2 or later to ensure `call_soon()` callbacks are executed as expected.
affects: nest-asyncio2 < v1.7.2
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'nest_asyncio2'
This error occurs when the 'nest_asyncio2' package is not installed in the Python environment.
fixInstall the package using pip: 'pip install nest-asyncio2'.
ImportError: cannot import name 'apply' from 'nest_asyncio2'
This error occurs when attempting to import the 'apply' function from 'nest_asyncio2' without the package being installed or due to a typo in the import statement.
fixEnsure that 'nest_asyncio2' is installed and use the correct import statement: 'from nest_asyncio2 import apply'.
RuntimeError: This event loop is already running
This error occurs when trying to run an asyncio event loop that is already running, which is common in environments like Jupyter notebooks.
fixApply the 'nest_asyncio2' patch to allow nested event loops: 'import nest_asyncio2; nest_asyncio2.apply()'.
ResourceWarning: unclosed event loop
This warning occurs when an event loop is created but not properly closed, leading to resource leaks.
fixTo avoid this warning, set 'run_close_loop=True' when applying the patch: 'nest_asyncio2.apply(run_close_loop=True)'.
ResourceWarning: unclosed event loop at exit
On Python 3.12+ and newer, `nest-asyncio2` (like its predecessor `nest_asyncio`) might create an event loop when `asyncio.run()` is called without an existing loop, but not explicitly close it, leading to this warning (which is often hidden by default).
fiximport nest_asyncio2
nest_asyncio2.apply(run_close_loop=True)
Upgrade
Version history
1.7.2latest on PyPI · released Feb 13, 2026
Audit
Dependencies
No dependency data recorded yet.