2. 使用 Python 解释器

2.1. 调用解释器

The Python interpreter is usually installed as /usr/local/bin/python 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

to the shell. 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.)

On Windows machines, the Python installation is usually placed in C:\Python27, 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:

set path=%path%;C:\python27

在主提示符中输入文件结束字符(在 Unix 系统中是 Control-D,Windows 系统中是 Control-Z)就退出解释器并返回退出状态为0。如果这样不管用,你还可以写这个命令退出:quit()

The interpreter’s line-editing features usually aren’t very sophisticated. On Unix, whoever installed the interpreter may have enabled support for the GNU readline library, which adds more elaborate interactive editing and history features. 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 交互式编辑和编辑历史 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.

解释器运行的时候有点像 Unix 命令行:在一个标准输入 tty 设备上调用,它能交互式地读取和执行命令;调用时提供文件名参数,或者有个文件重定向到标准输入的话,它就会读取和执行文件中的 脚本

另一种启动解释器的方式是 python -c command [arg] ...,其中 command 要换成想执行的指令,就像命令行的 -c 选项。由于 Python 代码中经常会包含对终端来说比较特殊的字符,通常情况下都建议用英文单引号把 command 括起来。

有些 Python 模块也可以作为脚本使用。可以这样输入:python -m module [arg] ...,这会执行 module 的源文件,就跟你在命令行把路径写全了一样。

在运行脚本的时候,有时可能也会需要在运行后进入交互模式。这种时候在文件参数前,加上选项 -i 就可以了。

All command-line options are described in 命令行与环境.

2.1.1. 传入参数

如果可能的话,解释器会读取命令行参数,转化为字符串列表存入 sys 模块中的 argv 变量中。执行命令 import sys 你可以导入这个模块并访问这个列表。这个列表最少也会有一个元素;如果没有给定输入参数,sys.argv[0] 就是个空字符串。如果脚本名是 '-'``(标准输入)时,``sys.argv[0] 就是 '-'。使用 -c 命令 时,sys.argv[0] 就会是 '-c'。如果使用选项 -m modulesys.argv[0] 就是包含目录的模块全名。在 -c command-m module 之后的选项不会被解释器处理,而会直接留在 sys.argv 中给命令或模块来处理。

2.1.2. 交互模式

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

python
Python 2.7 (#1, Feb 28 2010, 00:02:06)
Type "help", "copyright", "credits" or "license" for more information.
>>>

多行指令需要在连续的多行中输入。比如,以 if 为例:

>>> the_world_is_flat = 1
>>> if the_world_is_flat:
...     print "Be careful not to fall off!"
...
Be careful not to fall off!

有关交互模式的更多内容,请见 交互模式

2.2. 解释器的运行环境

2.2.1. 源文件的字符编码

By default, Python source files are treated as encoded in ASCII. 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 -*-

其中 encoding 可以是 Python 支持的任意一种 codecs

比如,要声明使用 Windows-1252 编码,你的源码文件要写成:

# -*- coding: cp1252 -*-

关于 第一行 规则的一种例外情况是,源码以 UNIX “shebang” 行 开头。这种情况下,编码声明就要写在文件的第二行。例如:

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