Thecode module provides facilities to implement read-eval-print loops inPython. Two classes and convenience functions are included which can be used tobuild applications which provide an interactive interpreter prompt.
This function is useful for programs that want to emulate Python’s interpretermain loop (a.k.a. the read-eval-print loop). The tricky part is to determinewhen the user has entered an incomplete command that can be completed byentering more text (as opposed to a complete command or a syntax error). Thisfunctionalmost always makes the same decision as the real interpreter mainloop.
source is the source string;filename is the optional filename from whichsource was read, defaulting to'<input>'; andsymbol is the optionalgrammar start symbol, which should be either'single' (the default) or'eval'.
Returns a code object (the same ascompile(source,filename,symbol)) if thecommand is complete and valid;None if the command is incomplete; raisesSyntaxError if the command is complete and contains a syntax error, orraisesOverflowError orValueError if the command contains aninvalid literal.
Compile and run some source in the interpreter. Arguments are the same as forcompile_command(); the default forfilename is'<input>', and forsymbol is'single'. One several things can happen:
The return value can be used to decide whether to usesys.ps1 orsys.ps2to prompt the next line.
Execute a code object. When an exception occurs,showtraceback() is calledto display a traceback. All exceptions are caught exceptSystemExit,which is allowed to propagate.
A note aboutKeyboardInterrupt: this exception may occur elsewhere inthis code, and may not always be caught. The caller should be prepared to dealwith it.
TheInteractiveConsole class is a subclass ofInteractiveInterpreter, and so offers all the methods of theinterpreter objects as well as the following additions.
Enter search terms or a module, class or function name.