002 使用 Python 解释器

2. 使用 Python 解释器

2.1. Python 解释器的调用

Python 解释器通常被安装在机器的 /usr/local/bin/python3.6 目录下,如果该目录是可用的话;用你的 Unix shell 搜索 /usr/local/bin 目录然后键入如下命令开始:[1]

python3.6

因为在安装时可以选择解释器所在的目录,所以 Python 解释器也可能在其他的目录下,这就需要联系你的 Python 顾问或系统管理员来询问 Python 解释器的所在位置。(比如 /usr/local/python 就是一个很受欢迎的安装路径。)

在运行 Windows 操作系统的计算机上,Python 通常被安装在 C:\Python36 文件夹中。当然,在你运行安装包的时候也可能改变它的位置。如果想把此路径添加到你的环境变量中,你可以在一个 DOS 命令框中键入如下命令提示符:

set path=%path%;C:\python36

在命令提示符中键入文件终止符号(在 Unix 系统中为 Control-D,在 Windows 系统中为 Control-Z)就能以零退出状态关闭解释器。 如果上述方法无效的话,你也可以通过键入如下命令退出解释器:

quit()

Python 解释器的线性编辑特性包括在支持读取代码的系统中实现交互编辑,历史替换和补全代码。查看是否支持命令行输入最有效的方法莫过于在你刚刚进入 Python 提示符的时候键入 Control-P。如果发出嘟嘟的声音,你就可以进行命令行编辑。相关关键字简介参见附录 Interactive Input Editing and History Substitution 。如果没有出现任何反馈效果,或者回应为 ^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.

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.

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.

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.

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

Continuation lines are needed when entering a multi-line construct. As an example, take a look at this if statement:

>>>
>>> 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: cp-1252 -*-

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: cp-1252 -*-

脚注

[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.
posted @ 2017-12-13 03:20  shuoliuchn  阅读(468)  评论(0编辑  收藏  举报