def do_py(self, arg):
self.pystate['<STR_LIT>'] = self<EOL>arg = arg.strip()<EOL>localvars = (self.locals_in_py and self.pystate) or {}<EOL>interp = InteractiveConsole(locals=localvars)<EOL>interp.runcode('<STR_LIT>')<EOL>if arg:<EOL><INDENT>interp.runcode(arg)<EOL><DEDENT>else:<EOL><INDENT>def quit():<EOL><INDENT>raise EmbeddedConsoleExit<EOL><DEDENT>def onecmd(arg):<EOL><INDENT>return self.onecmd(arg + '<STR_LIT:\n>')<EOL><DEDENT>def run(arg):<EOL><INDENT>try:<EOL><INDENT>f = open(arg)<EOL>interp.runcode(f.read())<EOL>f.close()<EOL><DEDENT>except IOError as e:<EOL><INDENT>self.perror(e)<EOL><DEDENT><DEDENT>self.pystate['<STR_LIT>'] = quit<EOL>self.pystate['<STR_LIT>'] = quit<EOL>self.pystate['<STR_LIT>'] = onecmd<EOL>self.pystate['<STR_LIT>'] = run<EOL>try:<EOL><INDENT>cprt = '<STR_LIT>'<EOL>keepstate = Statekeeper(sys, ('<STR_LIT>', '<STR_LIT>'))<EOL>sys.stdout = self.stdout<EOL>sys.stdin = self.stdin<EOL>interp.interact(banner="<STR_LIT>" %<EOL>(sys.version, sys.platform, cprt, self.__class__.__name__, self.do_py.__doc__))<EOL><DEDENT>except EmbeddedConsoleExit:<EOL><INDENT>pass<EOL><DEDENT>keepstate.restore()<EOL><DEDENT>
::

    Usage:
        py
        py COMMAND

    Arguments:
        COMMAND   the command to be executed

    Description:

        The command without a parameter will be executed and the
        interactive python mode is entered. The python mode can be
        ended with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows),
        ``quit()``,'`exit()``. Non-python commands can be issued with
        ``cmd("your command")``.  If the python code is located in an
        external file it can be run with ``run("filename.py")``.

        In case a COMMAND is provided it will be executed and the
        python interpreter will return to the command shell.

        This code is copied from Cmd2.