16. 附录 ¶
16.1. 交互模式 ¶
There are two variants of the interactive REPL . The classic basic interpreter is supported on all platforms with minimal line control capabilities.
On Windows, or Unix-like systems with
curses
support, a new interactive shell is used by default. This one supports color, multiline editing, history browsing, and paste mode. To disable color, see
Controlling color
for details. Function keys provide some additional functionality.
F1
enters the interactive help browser
pydoc
.
F2
allows for browsing command-line history with neither output nor the
>>>
and
…
prompts.
F3
enters “paste mode”, which makes pasting larger blocks of code easier. Press
F3
to return to the regular prompt.
When using the new interactive shell, exit the shell by typing exit or quit . Adding call parentheses after those commands is not required.
If the new interactive shell is not desired, it can be disabled via the
PYTHON_BASIC_REPL
环境变量。
16.1.1. 错误处理 ¶
When an error occurs, the interpreter prints an error message and a stack trace. In interactive mode, it then returns to the primary prompt; when input came from a file, it exits with a nonzero exit status after printing the stack trace. (Exceptions handled by an
except
clause in a
try
statement are not errors in this context.) Some errors are unconditionally fatal and cause an exit with a nonzero exit status; this applies to internal inconsistencies and some cases of running out of memory. All error messages are written to the standard error stream; normal output from executed commands is written to standard output.
Typing the interrupt character (usually
Control
-
C
or
删除
) to the primary or secondary prompt cancels the input and returns to the primary prompt.
[
1
]
Typing an interrupt while a command is executing raises the
KeyboardInterrupt
exception, which may be handled by a
try
语句。
16.1.2. 可执行 Python 脚本 ¶
On BSD’ish Unix systems, Python scripts can be made directly executable, like shell scripts, by putting the line
#!/usr/bin/env python3