Skip to content

[ipython] Ipython setup and startup time #1716

Closed

Description

@neoniobium

Startup time increased do to ipython import

Summary

Importing IPython increases startup of an Cmd2 app (on my system about 0.3–0.4 seconds).

Details

The following lines (A) (from cmd2/cmd2.py):

with contextlib.suppress(ImportError):
    from IPython import start_ipython

constitute about 1/3 (0.3–0.4s total on my system) of the total startup time for the getting_started example — what is noticeable.

I’m a bit puzzled by these lines, because start_ipython is only used in the do_ipy method of the Cmd class. However, inside this method, the function is imported again (B):

# Allow users to install ipython from a cmd2 prompt when needed and still have ipy command work
try:
    _dummy = start_ipython  # noqa: F823
except NameError:
    from IPython import start_ipython

At least on my linux system, removing lines (A) and only importing start_ipython (instead of (B)) doesn’t change anything, and it reduces the startup time as mentioned above.

I understand the comment in (B) that the intended purpose of this setup is that installing ipython from within a running cmd2 app (for example, via the !pip install ipython command) should make ipython usable without closing the app. Do I understand this correctly?

However, with or without the changes described above, this still seems possible for me—at least on Linux—when using a virtual environment with uv or pip directly.

Possible solution

The changes described above. However I cannot test whether they break anything on a different system like window, for example.

Additional note

I think it would be reasonable to provide ipython as its own optional dependency group in the pyproject.toml file.

Activity

  1. kmvanbrunt commented on Jul 16, 2026

    @kmvanbrunt
    Member

    That first import is a relic of a bug fix in 2024.

    The comment for it was:
    When using gnureadline with Python 3.13, start_ipython needs to be imported before any readline-related stuff

    During the conversion to prompt-toolkit, it looks like we removed the comment but left the import. I'm pretty sure we can remove it.

    There is 1 remaining case where gnureadline could be imported and that's when we enable tab completion in the py shell. I'll set up a 3.13 test environment later and see what happens if we remove the first start_ipython import.

  2. kmvanbrunt commented on Jul 16, 2026

    @kmvanbrunt
    Member

    For reference, here is the PR where that first start_ipython import was introduced.
    #1358

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions