Skip to content

Defer debugpy and psutil imports until they are actually needed - #1543

Draft
Carreau wants to merge 1 commit into
ipython:mainfrom
Carreau:lazy-debug-and-psutil
Draft

Defer debugpy and psutil imports until they are actually needed#1543
Carreau wants to merge 1 commit into
ipython:mainfrom
Carreau:lazy-debug-and-psutil

Conversation

@Carreau

@Carreau Carreau commented Aug 4, 2026

Copy link
Copy Markdown
Member

Both imports were paid for on every kernel startup even though most sessions never debug or ask for usage information.

  • IPythonKernel.debugger is now a lazily-created property; the debugger (and the debugpy import) is only built on the first debug request. poll_stopped_queue is scheduled at that point rather than in start().
  • debugger_class was a Type trait, which traitlets resolves — and therefore imports — as soon as the kernel is instantiated. It is replaced by a plain debugger_class_name string; debugger_class remains as a deprecated property, and subclasses still overriding it keep working with a DeprecationWarning.
  • psutil is imported through _get_psutil(), which caches the (possibly None) result on first use.

On a local test (where I have optimisation in IPython and traitlets as well), this brings the startup time from 220ms to 170ms

Both imports were paid for on every kernel startup even though most
sessions never debug or ask for usage information.

* `IPythonKernel.debugger` is now a lazily-created property; the
  debugger (and the debugpy import) is only built on the first debug
  request. `poll_stopped_queue` is scheduled at that point rather than
  in `start()`.
* `debugger_class` was a `Type` trait, which traitlets resolves — and
  therefore imports — as soon as the kernel is instantiated. It is
  replaced by a plain `debugger_class_name` string; `debugger_class`
  remains as a deprecated property, and subclasses still overriding it
  keep working with a DeprecationWarning.
* `psutil` is imported through `_get_psutil()`, which caches the
  (possibly None) result on first use.

On a local test (where I have optimisation in IPython and traitlets as
well), this brings the startup time from 220ms to 170ms
@Carreau
Carreau force-pushed the lazy-debug-and-psutil branch from 722618c to ac6ca05 Compare August 4, 2026 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant