2. 使用 Python 直譯器

2.1. 啟動直譯器

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's search path makes it possible to start it by typing the command:

python3.7

能啟動 Python 1。因為直譯器存放的目錄是個安裝選項,其他的目錄也是有可能的;請洽談在地的 Python 達人或者系統管理員。(例如:/usr/local/python 是個很常見的另類存放路徑。)

On Windows machines where you have installed Python from the Microsoft Store, the python3.7 command will be available. If you have the py.exe launcher installed, you can use the py command. See Excursus: Setting environment variables for other ways to launch Python.

在主提示符輸入一個 end-of-file 字元(在 Unix 上為 Control-D;在 Windows 上為 Control-Z)會使得直譯器以零退出狀況 (zero exit status) 離開。如果上述的做法沒有效,也可以輸入指令 quit() 離開直譯器。

The interpreter's line-editing features include interactive editing, history substitution and code completion on systems that support the GNU Readline library. 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

所有指令可用的參數都詳記在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:

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

接續多行的情況出現在需要多行才能建立完整指令時。舉例來說,像是 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. 原始碼的字元編碼 (encoding)

預設 Python 原始碼檔案的字元編碼使用 UTF-8。在這個編碼中,世界上多數語言的文字可以同時被使用在字串內容、識別名 (identifier) 及註解中 --- 雖然在標準函式庫中只使用 ASCII 字元作為識別名,這也是個任何 portable 程式碼需遵守的慣例。如果要正確地顯示所有字元,您的編輯器需要能夠認識檔案為 UTF-8,並且需要能顯示檔案中所有字元的字型。

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

在 Unix 中,Python 3.x 直譯器預設安裝不會以 python 作為執行檔名稱,以避免與 現有的 Python 2.x 執行檔名稱衝突。