The Python Tutorial(教程)2

2. Using the Python Interpreter

2.1. Invoking(调用) the Interpreter

The Python interpreter is usually installed(安装) as /usr/local/bin/python3.7 on those machines where it is available; putting /usr/local/bin in your Unix shell抯 search path makes it possible to start it by typing the command:

Python解释器通常在那些可用的机器上安装为/usr/local/bin/python3.7; 将/ usr / local / bin放在Unix shell的搜索路径中,可以通过输入命令来启动它:


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.)

到了shell。 [1]由于选择解释器所在的目录是一个安装选项,其他地方也是可能的; 请咨询您当地的Python大师或系统管理员。 (例如,/ usr / local / python是一个受欢迎的替代位置。)

On Windows machines, the Python installation is usually placed in C:\Python36, though you can change this when you’re running the installer. To add this directory to your path, you can type the following command into the command prompt in a DOS box:

在Windows机器上,Python安装通常放在C:\ Python36中,但您可以在运行安装程序时更改此设置。 要将此目录添加到路径,可以在DOS框中的命令提示符中键入以下命令:

set path=%path%;C:\python36

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)会导致解释器以零退出状态退出。 如果这不起作用,您可以通过键入以下命令退出解释器:quit()。

The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that support readline. 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.

解释器的行编辑功能包括支持readline的系统上的交互式编辑,历史替换和代码完成。 也许最快的检查是否支持命令行编辑是在你得到的第一个Python提示符下键入Control-P。 如果发出哔哔声,则可以进行命令行编辑; 有关键的介绍,请参阅附录交互式输入编辑和历史替换。 如果没有发生任何事情,或者如果^ P被回显,则命令行编辑不可用; 你只能使用退格键从当前行中删除字符。

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. 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] ...,它在命令中执行语句,类似于shell的-c选项。 由于Python语句通常包含空格或shell特有的其他字符,因此通常建议使用单引号引用命令。

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] ...调用,它执行模块的源文件,就像在命令行中拼写出它的全名一样

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. 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.

2.1.2. Interactive Mode互动模式

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:

$ python3.7
Python 3.7 (default, Sep 16 2015, 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 ifstatement:


>>> 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. 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.

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.

For example, to declare that Windows-1252 encoding is to be used, the first line of your source code file should be:

# -*- 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:

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


[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.

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 -*-