2. Using the Python Interpreter

2. 使用 Python 解释器



2.1. Invoking the Interpreter

2.1. 调用解释器

The Python interpreter is usually installed as "/usr/local/bin/python3.9" 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:
The Python interpreter is usually installed as"/usr/local/bin/python3.9" on those machines where it is available;putting "/usr/local/bin" in your Unix shell's search path makes itpossible to start it by typing the command:

   python3.9 
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. (E.g., "/usr/local/python" is a popular alternative location.)
就能运行了 [1] 。安装时可以选择安装目录,所以解释器也可能在别的地方;可以问问你身边的 Python 大牛,或者你的系统管理员。(比如"/usr/local/python" 也是比较常用的备选路径)

On Windows machines where you have installed from the Microsoft Store, the "python3.9" command will be available. If you have the py.exe launcher installed, you can use the "py" command. See Excursus: Setting environment variables for other ways to launch Python.
On Windows machines where you have installed from the Microsoft Store,the "python3.9" command will be available. If you have the py.exelauncher installed, you can use the "py" command. See 附录:设置环境变量 for other ways to launch Python.

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. If that doesn't work, you can exit the interpreter by typing the following command: "quit()".
在主提示符中输入文件结束字符(在 Unix 系统中是 "Control-D",Windows 系统中是 "Control-Z")就退出解释器并返回退出状态为0。如果这样不管用,你还可以写这个命令退出:"quit()"。

The interpreter's line-editing features include interactive editing, history substitution and code completion on systems that support the GNU Readline library. Perhaps the quickest check to see whether command line editing is supported is typing "Control-P" to the first Python prompt you get. If 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.
The interpreter's line-editing features include interactive editing,history substitution and code completion on systems that support theGNU Readline library. Perhaps the quickest check to see whethercommand line editing is supported is typing "Control-P" to the firstPython prompt you get. If it beeps, you have command line editing;see Appendix 交互式编辑和编辑历史 for an introduction to the keys. Ifnothing appears to happen, or if "^P" is echoed, command line editingisn't available; you'll only be able to use backspace to removecharacters from the current line.

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 命令行:在一个标准输入 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. 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 -c command [arg] ...",其中 command要换成想执行的指令,就像命令行的 "-c" 选项。由于 Python 代码中经常会包含对终端来说比较特殊的字符,通常情况下都建议用英文单引号把 command括起来。

Some Python modules are also useful as scripts. 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 模块也可以作为脚本使用。可以这样输入:"python -m module[arg] ...",这会执行 module 的源文件,就跟你在命令行把路径写全了一样。

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

2.1.1. 传入参数

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. You can access this list by executing "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. When the script name is given as "'-'" (meaning standard input), "sys.argv[0]" is set to "'-'". When "-c" command is used, "sys.argv[0]" is set to "'-c'". When "-m" module is used, "sys.argv[0]" is set to the full name of the located module. 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.
如果可能的话,解释器会读取命令行参数,转化为字符串列表存入 "sys" 模块中的 "argv" 变量中。执行命令 "import sys" 你可以导入这个模块并访问这个列表。这个列表最少也会有一个元素;如果没有给定输入参数,"sys.argv[0]"就是个空字符串。如果脚本名是标准输入,"sys.argv[0]" 就是 "'-'"。使用"-c" command 时,"sys.argv[0]" 就会是 "'-c'"。如果使用选项 "-m"module,"sys.argv[0]" 就是包含目录的模块全名。在 "-c" command 或"-m" module 之后的选项不会被解释器处理,而会直接留在 "sys.argv" 中给命令或模块来处理。



2.1.2. Interactive Mode

2.1.2. 交互模式

When commands are read from a tty, the interpreter is said to be in interactive mode. 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:
在终端(tty)输入并执行指令时,我们说解释器是运行在 交互模式(interactive mode)。在这种模式中,它会显示 主提示符(primary prompt),提示输入下一条指令,通常用三个大于号(">>>")表示;连续输入行的时候,它会显示 次要提示符,默认是三个点("...")。进入解释器时,它会先显示欢迎信息、版本信息、版权声明,然后就会出现提示符:

   $ python3.9 
Python 3.9 (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. 解释器的运行环境



2.2.1. Source Code Encoding

2.2.1. 源文件的字符编码

By default, Python source files are treated as encoded in 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. 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.
默认情况下,Python 源码文件以 UTF-8 编码方式处理。在这种编码方式中,世界上大多数语言的字符都可以同时用于字符串字面值、变量或函数名称以及注释中——尽管标准库中只用常规的 ASCII 字符作为变量或函数名,而且任何可移植的代码都应该遵守此约定。要正确显示这些字符,你的编辑器必须能识别 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 支持的任意一种 "codecs"。

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. In this case, the encoding declaration should be added as the second line of the file. For example:
关于 第一行 规则的一种例外情况是,源码以 UNIX "shebang" 行 开头。这种情况下,编码声明就要写在文件的第二行。例如:

   #!/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.
[1] 在Unix系统中,Python 3.x解释器默认安装后的执行文件并不叫作"python",这样才不会与同时安装的Python 2.x冲突。

results matching ""

    No results matching ""