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.
Startup time increased do to ipython import
Summary
Importing
IPythonincreases startup of anCmd2app (on my system about 0.3–0.4 seconds).Details
The following lines (A) (from
cmd2/cmd2.py):constitute about 1/3 (0.3–0.4s total on my system) of the total startup time for the
getting_startedexample — what is noticeable.I’m a bit puzzled by these lines, because
start_ipythonis only used in thedo_ipymethod of theCmdclass. However, inside this method, the function is imported again (B):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
ipythonfrom within a runningcmd2app (for example, via the!pip install ipythoncommand) should makeipythonusable 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
uvorpipdirectly.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
ipythonas its own optional dependency group in thepyproject.tomlfile.