16. Appendix附录¶
16.1. Interactive Mode交互模式¶
16.1.1. Error Handling错误处理¶
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.) try
语句中的except
子句处理的异常在此上下文中不是错误。)Some errors are unconditionally fatal and cause an exit with a nonzero exit; 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 Delete) to the primary or secondary prompt cancels the input and returns to the primary prompt. 在主提示或辅助提示中键入中断字符(通常为Control-C或Delete)将取消输入并返回主提示。1 Typing an interrupt while a command is executing raises the 在执行命令时键入中断会引发KeyboardInterrupt
exception, which may be handled by a try
statement.KeyboardInterrupt
异常,该异常可能由try
语句处理。
16.1.2. Executable Python Scripts可执行Python脚本¶
On BSD’ish Unix systems, Python scripts can be made directly executable, like shell scripts, by putting the line在BSD'ish Unix系统上,通过在
#!/usr/bin/env python3.5
(assuming that the interpreter is on the user’s (假设解释器位于用户PATH
) at the beginning of the script and giving the file an executable mode. PATH
上)并为文件提供可执行模式。The #!
must be the first two characters of the file. #!
必须是文件的前两个字符。On some platforms, this first line must end with a Unix-style line ending (在某些平台上,第一行必须以Unix样式的行结尾('\n'
), not a Windows ('\r\n'
) line ending. '\n'
),而不是Windows('\r\n'
)行结尾。Note that the hash, or pound, character, 请注意,在Python中,哈希或磅字符'#'
, is used to start a comment in Python.'#'
用于开始注释。
The script can be given an executable mode, or permission, using the chmod command.可以使用chmod命令为脚本提供可执行模式或权限。
$ chmod +x myscript.py
On Windows systems, there is no notion of an “executable mode”. 在Windows系统上,没有“可执行模式”的概念。The Python installer automatically associates Python安装程序自动将.py
files with python.exe
so that a double-click on a Python file will run it as a script. .py
文件与python.exe
相关联,以便双击Python文件将其作为脚本运行。The extension can also be 扩展名也可以是.pyw
, in that case, the console window that normally appears is suppressed..pyw
,在这种情况下,通常显示的控制台窗口将被抑制。
16.1.3. The Interactive Startup File交互式启动文件¶
When you use Python interactively, it is frequently handy to have some standard commands executed every time the interpreter is started. 当您以交互方式使用Python时,每次启动解释器时执行一些标准命令通常很方便。You can do this by setting an environment variable named 可以通过将名为PYTHONSTARTUP
to the name of a file containing your start-up commands. PYTHONSTARTUP
的环境变量设置为包含启动命令的文件名来实现这一点。This is similar to the 这类似于Unix shell的.profile
feature of the Unix shells..profile
功能。
This file is only read in interactive sessions, not when Python reads commands from a script, and not when 此文件仅在交互式会话中读取,而不是在Python从脚本读取命令时读取,也不是在将/dev/tty
is given as the explicit source of commands (which otherwise behaves like an interactive session). /dev/tty
作为命令的显式源时读取(否则其行为类似于交互式会话)。It is executed in the same namespace where interactive commands are executed, so that objects that it defines or imports can be used without qualification in the interactive session. 它在执行交互命令的同一命名空间中执行,因此它定义或导入的对象可以在交互会话中无需限定即可使用。You can also change the prompts 您还可以更改此文件中的提示sys.ps1
and sys.ps2
in this file.sys.ps1
和sys.ps2
。
If you want to read an additional start-up file from the current directory, you can program this in the global start-up file using code like 如果要从当前目录读取其他启动文件,可以在全局启动文件中使用类似if os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read())
. if os.path.isfile('.pythonrc.py'): exec(open('.pythonrc.py').read())
的代码对其进行编程。If you want to use the startup file in a script, you must do this explicitly in the script:如果要在脚本中使用启动文件,必须在脚本中明确执行此操作:
import os
filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename):
with open(filename) as fobj:
startup_file = fobj.read()
exec(startup_file)
16.1.4. The Customization Modules自定义模块¶
Python provides two hooks to let you customize it: Python提供了两个钩子来让您自定义它:sitecustomize
and usercustomize
. sitecustomize
和usercustomize
。To see how it works, you need first to find the location of your user site-packages directory. 要了解其工作原理,首先需要找到用户站点包目录的位置。Start Python and run this code:启动Python并运行以下代码:
>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.5/site-packages'
Now you can create a file named 现在,您可以在该目录中创建一个名为usercustomize.py
in that directory and put anything you want in it. usercustomize.py
的文件,并将任何需要的内容放入其中。It will affect every invocation of Python, unless it is started with the 它将影响Python的每次调用,除非它以-s
option to disable the automatic import.-s
选项启动以禁用自动导入。
sitecustomize
works in the same way, but is typically created by an administrator of the computer in the global site-packages directory, and is imported before 工作方式相同,但通常由计算机管理员在全局site-packages目录中创建,并在usercustomize
. usercustomize
之前导入。See the documentation of the 有关更多详细信息,请参阅site
module for more details.site
模块的文档。
Footnotes
- 1
A problem with the GNU Readline package may prevent this.