Embedded Python Shells¶
cmd2.Cmd class is instantiated with
include_py=True, then the
py command will be present and run an interactive Python shell:
from cmd2 import Cmd class App(Cmd): def __init__(self): Cmd.__init__(self, include_py=True)
The Python shell can run CLI commands from you application using the object
self.pyscript_name (defaults to
app). This wrapper provides
access to execute commands in your
cmd2 application while maintaining
isolation from the full Cmd instance. For example, any application command
can be run with
You may optionally enable full access to to your application by setting
True. Enabling this flag adds
self to the
python session, which is a reference to your
cmd2 application. This can be
useful for debugging your application.
Any local or global variable created within the Python session will not persist in the CLI’s environment.
self.py_locals is always available in the Python environment.
All of these parameters are also available to Python scripts which run in your
application via the
- supports tab completion of file system paths
- has the ability to pass command-line arguments to the scripts invoked
This command provides a more complicated and more powerful scripting capability
than that provided by the simple text file scripts. Python scripts can include
conditional control flow logic. See the python_scripting.py
application and the script_conditional.py script in the
code directory for an example of how to achieve this in your own applications.
See Scripting for an explanation of both scripting
methods in cmd2 applications.
A simple example of using
run_pyscript is shown below along with the
(Cmd) run_pyscript examples/scripts/arg_printer.py foo bar baz Running Python script 'arg_printer.py' which was called with 3 arguments arg 1: 'foo' arg 2: 'bar' arg 3: 'baz'
If IPython is installed on the system and the
cmd2.Cmd class is
include_ipy=True, then the optional
ipy command will
run an interactive IPython shell:
from cmd2 import Cmd class App(Cmd): def __init__(self): Cmd.__init__(self, include_ipy=True)
ipy command enters an interactive IPython session. Similar to an
interactive Python session, this shell can access your application instance via
True and any changes to your application
self will persist. However, any local or global variable created
ipy shell will not persist in the CLI’s environment
Also, as in the interactive Python session, the
ipy shell has access to the
self.py_locals and can call back into the application using the
app object (or your custom name).
IPython provides many advantages, including:
- Comprehensive object introspection
- Get help on objects with
- Extensible tab completion, with support by default for completion of python variables and keywords
- Good built-in ipdb debugger
The object introspection and tab completion make IPython particularly efficient for debugging as well as for interactive experimentation and data analysis.