2. Using the Python Interpreter使用Python解释器

2.1. Invoking the Interpreter调用解释器

The Python interpreter is usually installed as /usr/local/bin/python3.10 on those machines where it is available; putting /usr/local/bin in your Unix shell’s search path makes it possible to start it by typing the command:Python解释器通常在可用的机器上安装为/usr/local/bin/python3.10;将/usr/local/bin放入Unix shell的搜索路径,可以通过键入以下命令来启动shell:

python3.10

to the shell. 1 Since the choice of the directory where the interpreter lives is an installation option, other places are possible; check with your local Python guru or system administrator. 由于选择解释器所在的目录是一个安装选项,所以其他地方也是可能的;请咨询当地的Python专家或系统管理员。(E.g., /usr/local/python is a popular alternative location.)(例如,/usr/local/python是一个流行的替代位置。)

On Windows machines where you have installed Python from the Microsoft Store, the python3.10 command will be available. 在已从Microsoft应用商店安装Python的Windows计算机上,python3.10号命令将可用。If you have the py.exe launcher installed, you can use the py command. 如果安装了py.exe启动器,可以使用py命令。See Excursus: Setting environment variables for other ways to launch Python.有关启动Python的其他方法,请参阅Excursus:设置环境变量

Typing an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt causes the interpreter to exit with a zero exit status. 在主提示符处键入文件结尾字符(Unix上为Control-D,Windows上为Ctrl-Z),将导致解释器以零退出状态退出。If that doesn’t work, you can exit the interpreter by typing the following command: quit().如果不起作用,可以通过键入以下命令退出解释器:quit()

The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that support the GNU Readline library. 解释器的行编辑功能包括在支持GNU Readline库的系统上进行交互式编辑、历史替换和代码完成。Perhaps the quickest check to see whether command line editing is supported is typing Control-P to the first Python prompt you get. 要查看是否支持命令行编辑,最快的检查可能是在第一个Python提示中键入Control-PIf it beeps, you have command line editing; see Appendix Interactive Input Editing and History Substitution for an introduction to the keys. 如果它发出哔哔声,您可以进行命令行编辑;参见附录交互式输入编辑和历史替换,了解按键的介绍。If nothing appears to happen, or if ^P is echoed, command line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.如果似乎什么也没有发生,或者如果^P被回显,则命令行编辑不可用;您只能使用backspace从当前行中删除字符。

The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and executes a script from that file.解释器的操作有点像Unix shell:当使用连接到tty设备的标准输入调用时,它以交互方式读取和执行命令;当使用文件名参数或文件作为标准输入调用时,它会从该文件读取并执行脚本

A second way of starting the interpreter is python -c command [arg] ..., which executes the statement(s) in command, analogous to the shell’s -c option. 启动解释器的第二种方法是python -c command [arg] ...,它执行命令中的语句,类似于shell的-c选项。Since Python statements often contain spaces or other characters that are special to the shell, it is usually advised to quote command in its entirety with single quotes.由于Python语句通常包含空格或shell特有的其他字符,因此通常建议使用单引号将整个命令括起来。

Some Python modules are also useful as scripts. 一些Python模块作为脚本也很有用。These can be invoked using python -m module [arg] ..., which executes the source file for module as if you had spelled out its full name on the command line.可以使用python -m module [arg] ...,它执行模块的源文件,就像您在命令行上拼出了它的全名一样。

When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. 当使用脚本文件时,有时能够运行脚本并在之后进入交互模式非常有用。This can be done by passing -i before the script.这可以通过在脚本之前传递-i来完成。

All command line options are described in Command line and environment.命令行和环境中描述了所有命令行选项。

2.1.1. Argument Passing参数传递

When known to the interpreter, the script name and additional arguments thereafter are turned into a list of strings and assigned to the argv variable in the sys module. 解释器知道后,脚本名及其后的附加参数将转换为字符串列表,并分配给sys模块中的argv变量。You can access this list by executing import sys. 您可以通过执行import sys来访问此列表。The length of the list is at least one; when no script and no arguments are given, sys.argv[0] is an empty string. 列表的长度至少为一;当没有脚本和参数时,sys.argv[0]是一个空字符串。When the script name is given as '-' (meaning standard input), sys.argv[0] is set to '-'. 当脚本名为'-'(表示标准输入)时,sys.argv[0]被设置为'-'When -c command is used, sys.argv[0] is set to '-c'. 当使用-c命令时,sys.argv[0]被设置为'-c'When -m module is used, sys.argv[0] is set to the full name of the located module. 当使用-m模块时,sys.argv[0]被设置为所定位模块的全名。Options found after -c command or -m module are not consumed by the Python interpreter’s option processing but left in sys.argv for the command or module to handle.-c命令-m模块之后找到的选项不会被Python解释器的选项处理使用,而是留在sys.argv中,供命令或模块处理。

2.1.2. Interactive Mode交互模式

When commands are read from a tty, the interpreter is said to be in interactive mode. 当从tty读取命令时,称解释器处于交互模式In this mode it prompts for the next command with the primary prompt, usually three greater-than signs (>>>); for continuation lines it prompts with the secondary prompt, by default three dots (...). 在这种模式下,它会用主提示符提示下一个命令,通常是三个大于号(>>>);对于续行,它会使用辅助提示进行提示,默认情况下是三个点(...)。The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt:在打印第一个提示之前,解释器打印一条欢迎信息,说明其版本号和版权声明:

$ python3.10
Python 3.10 (default, June 4 2019, 09:25:04)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Continuation lines are needed when entering a multi-line construct. 当输入多行构造时,需要连续行。As an example, take a look at this if statement:作为一个例子,请看下面的if语句:

>>> the_world_is_flat = True
>>> if the_world_is_flat:
... print("Be careful not to fall off!")
...
Be careful not to fall off!

For more on interactive mode, see Interactive Mode.有关交互模式的详细信息,请参阅交互模式

2.2. The Interpreter and Its Environment解释器及其环境

2.2.1. Source Code Encoding源代码编码

By default, Python source files are treated as encoded in UTF-8. 默认情况下,Python源文件被视为以UTF-8编码。In that encoding, characters of most languages in the world can be used simultaneously in string literals, identifiers and comments — although the standard library only uses ASCII characters for identifiers, a convention that any portable code should follow. 在这种编码中,世界上大多数语言的字符都可以在字符串文本、标识符和注释中同时使用——尽管标准库只使用ASCII字符作为标识符,这是任何可移植代码都应该遵循的约定。To display all these characters properly, your editor must recognize that the file is UTF-8, and it must use a font that supports all the characters in the file.要正确显示所有这些字符,编辑器必须识别文件为UTF-8,并且必须使用支持文件中所有字符的字体。

To declare an encoding other than the default one, a special comment line should be added as the first line of the file. 要声明默认编码以外的编码,应在文件的第一行添加一个特殊的注释行。 The syntax is as follows:语法如下:

# -*- coding: encoding -*-

where encoding is one of the valid codecs supported by Python.其中encoding是Python支持的有效编解码器之一。

For example, to declare that Windows-1252 encoding is to be used, the first line of your source code file should be:例如,要声明使用Windows-1252编码,源代码文件的第一行应该是:

# -*- coding: cp1252 -*-

One exception to the first line rule is when the source code starts with a UNIX “shebang” line. 第一行规则的一个例外是源代码以UNIX“shebang”行开头。In this case, the encoding declaration should be added as the second line of the file. 在这种情况下,应该将编码声明添加为文件的第二行。For example:例如:

#!/usr/bin/env python3
# -*- coding: cp1252 -*-

Footnotes

1

On Unix, the Python 3.x interpreter is by default not installed with the executable named python, so that it does not conflict with a simultaneously installed Python 2.x executable.在Unix上,Python 3x解释器默认情况下不与名为python的可执行文件一起安装,因此它不会与同时安装的Python 2x可执行文件冲突。