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
在主提示符輸入一個 end-of-file 字元(在 Unix 上為 Control-D;在 Windows 上為 Control-Z)會使得直譯器以零退出狀況 (zero exit status) 離開。如果上述的做法沒有效,也可以輸入指令 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 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.
這個直譯器使用起來像是 Unix shell:如果它被呼叫時連結至一個 tty 裝置,它會互動式地讀取並執行指令;如果被呼叫時給定檔名為引數或者使用 stdin 傳入檔案內容,它會將這個檔案視為腳本來閱讀。
另一個起動直譯器的方式為 python -c command [arg] ...
,它會執行在 command 裡的指令(們),行為如同 shell 的 -c
選項。因為 Python 的指令包含空白等 shell 用到的特殊字元,通常建議用單引號把 command 包起來。
有些 Python 模組使用上如腳本般一樣方便。透過 python -m module [arg] ...
可以執行 module 模組的原始碼,就如同直接傳入那個模組的完整路徑一樣的行為。
當要執行一個腳本檔時,有時候會希望在腳本結束時進入互動模式。此時可在執行腳本的指令加入 -i
。
All command-line options are described in Command line and environment.
2.1.1. 傳遞引數¶
當直擇器收到腳本的名稱和額外的引數後,他們會轉換為由字串所組成的 list(串列)並指派給 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 的選項不會被 Python 直譯器吸收掉,而是留在 sys.argv
變數中給後續的 command 或 module 使用。
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:
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. 原始碼的字元編碼 (encoding)¶
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 -*-
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 python
# -*- coding: cp1252 -*-