13. Editing and history of interactive input
Some versions of the Python interpreter support editing the current input line and history, similar to what you see in the Korn shell and the GNU Bash shell. This is implemented using the GNU ReadLine Library, which supports EMACS-style and VI-style editing. This library has its own documentation, which I don't repeat here , But the rationale is easy to explain. the interactive editing and history features described in this chapter are optional in the Unix and Cygwin editions.
13.1. Line editing
if supported, the input line can always be edited, whether the interpreter prints the main prompt or a subordinate prompt. You can edit the current line using the traditional Emacs control character. the most important of these are: C-a (CONTROL-A) moves the cursor to the beginning of the line,c-e moves to the end of the row,c-b moves the cursor one position to the left, andc-f moves one position to the right. The backspace key removes the character to the left of the cursor,c-d the character to the right of the cursor. c-k deletes all the characters to the right of the cursor in a row,c-y Pastes the last deleted string to the cursor position. the C-underline reverses the most recent change; It can be repeated and produces a cumulative effect.
13.2. Historical records
The history works in the following ways. all non-null input rows are saved in the history buffer, and when a new prompt is given, you are at the bottom of the buffer with a new line. c-p moves the row in the history buffer up (back),c-n down one line. any row in the history buffer can be edited, preceded by an asterisk to mark the modified line. pressing the Return key passes the current line to the interpreter. c-r began to search backwards;c-s began to search forward.
2.1 Invoking the interpreter
On the available machines, the Python interpreter is usually installed as /usr/local/bin/python; add /usr/local/bin to your UNIX shell search path. So that you can type commands in the shell by
Python
to start it. because the directory where the interpreter is placed is an installation option, other places are also possible; please contact your local python expert or system administrator. (for example,/usr/local/python is another common location.) )
on Windows machines, Python is usually installed in C:\Python27, and of course you can make changes when you run the Setup program. You can add this directory to the path by typing the following command at a command prompt in a DOS window:
Set path=%path%; C:\python27
Typing the end-of-file character at the main prompt ( control-don Unix, control-zon Windows) causes the interpreter to exit with a 0 exit status. If that doesn't work, you can exit the interpreter by typing the following command:quit ().
the line editing capabilities of the interpreter are usually not very complex. on Unix, regardless of who installed it, the interpreter might have enabled support for the GNU ReadLine Library, which added more detailed interactive editing and history features. the quickest way to check if command-line editing is supported is perhaps to type control-p for your first Python prompt. If it beeps, there is a command-line edit; See the Appendix for interactive input of Edit and history overrides for an introduction to shortcut keys. If nothing happens, or if ^pis displayed, command-line editing is not available; you can only use BACKSPACE to delete characters from the current line.
The interpreter is somewhat like a Unix shell: when invoked, it reads and executes commands interactively, using the connection to a TTY device as a standard input; when called with a filename parameter or file as a standard input, it reads and executes the script in the file.
the second way to start the interpreter is python- C command [arg ]..., which executes the statements in the command, similar to the Shell's - c option. Because Python statements often contain spaces or other shell special characters, it is generally advisable to put all the command in single quotes.
Some Python modules are also executable scripts. These modules can use the python- m module [ arg] ... Called directly, which is the same as the source file that executes the module on the command line by entering the full path name.
Sometimes it is useful to have a script file that can enter interactive mode after running the script. This can be done by preceding the script with the-i option.
Python interpreter shortcut keys