Debian Codegraph @

docs/features/embedded_python_shells.md

raw symbols SCIP
1 # Embedded Python Shells
2
3 ## Python (optional)
4
5 If the [cmd2.Cmd][] class is instantiated with `include_py=True`, then the optional `py` command
6 will be present and run an interactive Python shell:
7
8 ```py
9 from cmd2 import Cmd
10 class App(Cmd):
11 def __init__(self):
12 Cmd.__init__(self, include_py=True)
13 ```
14
15 The Python shell can run CLI commands from you application using the object named in
16 `self.pyscript_name` (defaults to `app`). This wrapper provides access to execute commands in your
17 `cmd2` application while maintaining isolation from the full `Cmd` instance. For example, any
18 application command can be run with `app("command ...")`.
19
20 You may optionally enable full access to your application by setting `self.self_in_py` to `True`.
21 Enabling this flag adds `self` to the python session, which is a reference to your `cmd2`
22 application. This can be useful for debugging your application.
23
24 Any local or global variable created within the Python session will not persist in the CLI's
25 environment.
26
27 Anything in `self.py_locals` is always available in the Python environment.
28
29 All of these parameters are also available to Python scripts which run in your application via the
30 `run_pyscript` command:
31
32 - supports tab completion of file system paths
33 - has the ability to pass command-line arguments to the scripts invoked
34
35 This command provides a more complex and powerful scripting capability than that provided by the
36 simple text file scripts. Python scripts can include conditional control flow logic. See
37 [python_scripting.py](https://github.com/python-cmd2/cmd2/blob/main/examples/python_scripting.py)
38 `cmd2` and the
39 [conditional.py](https://github.com/python-cmd2/cmd2/blob/main/examples/scripts/conditional.py)
40 script for an example of how to achieve this in your own applications. See
41 [Scripting](./scripting.md) for an explanation of both scripting methods in `cmd2` applications.
42
43 A simple example of using `run_pyscript` is shown below along with the
44 [arg_printer](https://github.com/python-cmd2/cmd2/blob/main/examples/scripts/arg_printer.py) script:
45
46 ```sh
47 (Cmd) run_pyscript examples/scripts/arg_printer.py foo bar baz
48 Running Python script 'arg_printer.py' which was called with 3 arguments
49 arg 1: 'foo'
50 arg 2: 'bar'
51 arg 3: 'baz'
52 ```
53
54 ## IPython (optional)
55
56 **If** [IPython](http://ipython.readthedocs.io) is installed on the system **and** the `cmd2.Cmd`
57 class is instantiated with `include_ipy=True`, then the optional `ipy` command will run an
58 interactive IPython shell:
59
60 ```py
61 from cmd2 import Cmd
62 class App(Cmd):
63 def __init__(self):
64 Cmd.__init__(self, include_ipy=True)
65 ```
66
67 The `ipy` command enters an interactive [IPython](http://ipython.readthedocs.io) session. Similar to
68 an interactive Python session, this shell can access your application instance via `self` if
69 `self.self_in_py` is `True` and any changes to your application made via `self` will persist.
70 However, any local or global variable created within the `ipy` shell will not persist in the CLI's
71 environment
72
73 Also, as in the interactive Python session, the `ipy` shell has access to the contents of
74 `self.py_locals` and can call back into the application using the `app` object (or your custom
75 name).
76
77 [IPython](http://ipython.readthedocs.io) provides many advantages, including:
78
79 > - Comprehensive object introspection
80 > - Get help on objects with `?`
81 > - Extensible tab completion, with support by default for completion of python variables and
82 > keywords
83 > - Good built-in [ipdb](https://pypi.org/project/ipdb/) debugger
84
85 The object introspection and tab completion make IPython particularly efficient for debugging as
86 well as for interactive experimentation and data analysis.