diff --git a/Lib/pdb.py b/Lib/pdb.py index 458eb8352652366..c3440a5d11a588a 100644 --- a/Lib/pdb.py +++ b/Lib/pdb.py @@ -84,7 +84,6 @@ import signal import socket import typing -import asyncio import inspect import weakref import builtins @@ -102,6 +101,8 @@ from types import CodeType from warnings import deprecated +lazy import asyncio + try: import _pyrepl.utils except ModuleNotFoundError: @@ -902,6 +903,10 @@ def _hold_exceptions(self, exceptions): self._chained_exception_index = 0 def _get_asyncio_task(self): + # If asyncio has never been imported there cannot be a running task, + # so skip the import rather than pay for it on every interaction. + if 'asyncio' not in sys.modules: + return None try: task = asyncio.current_task() except RuntimeError: diff --git a/Misc/NEWS.d/next/Library/2026-08-31-11-40-00.gh-issue-156774.Pd8Lz1.rst b/Misc/NEWS.d/next/Library/2026-08-31-11-40-00.gh-issue-156774.Pd8Lz1.rst new file mode 100644 index 000000000000000..f5f64c494296ccf --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-31-11-40-00.gh-issue-156774.Pd8Lz1.rst @@ -0,0 +1,2 @@ +Speed up :mod:`pdb` startup by only importing :mod:`asyncio` when the program +being debugged has already imported it.