2. 使用 Python 解释器
*********************


2.1. 调用解释器
===============

The Python interpreter is usually installed as
"/usr/local/bin/python3.6" 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:

   python3.6

就能运行了 [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:

   set path=%path%;C:\python36

在主提示符中输入文件结束字符（在 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
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 交
互式编辑和编辑历史 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" 就可以了。

关于所有的命令行选项，请参考 命令行与环境。


2.1.1. 传入参数
---------------

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


2.1.2. 交互模式
---------------

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

   $ python3.6
   Python 3.6 (default, Sep 16 2015, 09:25:04)
   [GCC 4.8.2] on linux
   Type "help", "copyright", "credits" or "license" for more information.
   >>>

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

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

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


2.2. 解释器的运行环境
=====================


2.2.1. 源文件的字符编码
-----------------------

默认情况下，Python 源码文件以 UTF-8 编码方式处理。在这种编码方式中，世
界上大多数语言的字符都可以同时用于字符串字面值、变量或函数名称以及注释
中——尽管标准库中只用常规的 ASCII 字符作为变量或函数名，而且任何可移植
的代码都应该遵守此约定。要正确显示这些字符，你的编辑器必须能识别 UTF-8
编码，而且必须使用能支持打开的文件中所有字符的字体。

如果不使用默认编码，要声明文件所使用的编码，文件的 *第一* 行要写成特殊
的注释。语法如下所示：

   # -*- coding: encoding -*-

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

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

   # -*- coding: cp1252 -*-

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

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

-[ 备注 ]-

[1] 在Unix系统中，Python 3.x解释器默认安装后的执行文件并不叫作
    "python"，这样才不会与同时安装的Python 2.x冲突。
