From 0bc51f2aacc328ea8abff9b4634abd551a850507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Krassowski?= <5832902+krassowski@users.noreply.github.com> Date: Mon, 25 Mar 2024 16:47:36 +0000 Subject: [PATCH] Do not import debugger/debugpy unless needed (#1223) --- ipykernel/ipkernel.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ipykernel/ipkernel.py b/ipykernel/ipkernel.py index 15242933..dabb0c44 100644 --- a/ipykernel/ipkernel.py +++ b/ipykernel/ipkernel.py @@ -21,7 +21,6 @@ from .comm.comm import BaseComm from .comm.manager import CommManager from .compiler import XCachingCompiler -from .debugger import Debugger, _is_debugpy_available from .eventloops import _use_appnope from .iostream import OutStream from .kernelbase import Kernel as KernelBase @@ -75,9 +74,7 @@ class IPythonKernel(KernelBase): help="Set this flag to False to deactivate the use of experimental IPython completion APIs.", ).tag(config=True) - debugpy_socket = ( - Instance(zmq.asyncio.Socket, allow_none=True) if _is_debugpy_available else None - ) + debugpy_socket = Instance(zmq.asyncio.Socket, allow_none=True) user_module = Any() @@ -107,6 +104,8 @@ def __init__(self, **kwargs): self.executing_blocking_code_in_main_shell = False + from .debugger import Debugger, _is_debugpy_available + # Initialize the Debugger if _is_debugpy_available: self.debugger = Debugger( @@ -214,6 +213,8 @@ async def process_debugpy(self): tg.cancel_scope.cancel() async def receive_debugpy_messages(self): + from .debugger import _is_debugpy_available + if not _is_debugpy_available: return @@ -221,6 +222,8 @@ async def receive_debugpy_messages(self): await self.receive_debugpy_message() async def receive_debugpy_message(self, msg=None): + from .debugger import _is_debugpy_available + if not _is_debugpy_available: return @@ -506,6 +509,8 @@ def do_complete(self, code, cursor_pos): async def do_debug_request(self, msg): """Handle a debug request.""" + from .debugger import _is_debugpy_available + if _is_debugpy_available: return await self.debugger.process_request(msg) return None