簡介¶
The Python standard library consists of a collection of modules. There are many ways to dissect this collection. Most modules are written in Python, but some are written in C. All can be imported into your program to add functionality. Some modules provide interfaces that are highly specific to Python, like printing a stack trace; some provide interfaces that are specific to particular operating systems, such as access to specific hardware; others provide interfaces that are specific to a particular application domain, like web development. Some modules are available in all versions and ports of Python; others are only available when the underlying system supports or requires them; yet others are available only when a particular configuration option was chosen at the time when Python was compiled and installed.
If you start reading this manual from the start, and skip to the next chapter when you get bored, you will get a reasonable overview of the available modules and application areas that are supported by the Python library. Of course, you don't have to read it like a novel --- you can also browse the table of contents (in front of the manual), or look for a specific function, module or term in the index (in the back). And finally, if you enjoy learning about random subjects, you choose a random page and read a section or two. Regardless of the order in which you read the sections of this manual, it helps to first read Built-in Functions, as the remainder of this section assumes familiarity with this material.
也參考
The built-in functions and classes (which can be used without an
import statement) are described in Python built-ins reference.
可用性之標註¶
如果出現「適用:Unix」標註,則代表該函式普遍存在於 Unix 系統中,但這並不保證其存在於某特定作業系統。
如果沒有分別標註的話,有標明「適用:Unix」標註的所有函式也都於 macOS、iOS 和 Android 上支援,因其建於 Unix 核心之上。
如果可用性標註同時包含最低核心 (Kernel) 版本及最低 libc 版本,則兩項條件必須同時符合。例如,標註為 適用:Linux >= 3.17 與 glibc >= 2.27 的功能需要 Linux 3.17 或更新版本以及 glibc 2.27 或更新版本。
WebAssembly 平台¶
WebAssembly 平台 wasm32-emscripten (Emscripten) 及 wasm32-wasi (WASI) 提供一組 POSIX API 的子集。WebAssembly 執行環境 (runtime) 與瀏覽器均採用沙盒機制,並限制對主機與外部資源的存取。任何使用到行程 (process)、執行緒 (threading)、網路、訊號 (signal) 或其他形式行程間通訊 (IPC) 的 Python 標準函式庫模組,皆可能無法使用,或無法像在其他類 Unix 系統上正常運作。檔案 I/O、檔案系統以及與 Unix 權限相關的函式亦有限制。Emscripten 不允許阻塞式 (blocking) I/O。其他阻塞操作(如 sleep())則會阻塞瀏覽器的事件迴圈 (event loop)。
Python 在 WebAssembly 平台上的特性與行為取決於 Emscripten-SDK 或 WASI-SDK 的版本、WASM 執行環境(瀏覽器、NodeJS、wasmtime)以及 Python 的編譯時期 (build time) 旗標。WebAssembly、Emscripten 及 WASI 均為不斷演進中的標準;某些功能(如網路)未來可能獲得支援。
若要在瀏覽器中使用 Python,使用者應考慮 Pyodide 或 PyScript。PyScript 建構於 Pyodide 之上,而 Pyodide 本身則建構於 CPython 與 Emscripten 之上。Pyodide 提供存取瀏覽器 JavaScript 與 DOM API 的管道,並透過 JavaScript 的 XMLHttpRequest 與 Fetch API 提供有限的網路功能。
與行程 (process) 相關的 API 皆無法使用,或一律回傳錯誤。這包含建立新行程(
fork()、execve())、等待行程(waitpid())、發送訊號(kill())或以其他方式與行程互動的 API。雖然可以引入 (import)subprocess模組,但它無法正常運作。socket模組雖可使用,但功能受限且行為與其他平台不同。在 Emscripten 上,socket 一律為非阻塞的 (non-blocking),且需要在伺服器端搭配額外的 JavaScript 程式碼與輔助工具來透過 WebSocket 代理 TCP;詳情請參閱 Emscripten Networking。WASI 快照預覽 1 (snapshot preview 1) 僅允許透過現有的檔案描述器 (file descriptor) 使用 socket。部分函式僅為虛設常式 (stub),它們不執行任何操作,且一律回傳固定的 (hardcoded) 數值。
與檔案描述器 (file descriptor)、檔案權限、檔案所有權及連結相關的函式均受限,且不支援某些操作。例如,WASI 不允許使用絕對路徑檔名的符號連結 (symlink)。
行動平台¶
在大多數方面,Android 與 iOS 皆屬於 POSIX 作業系統。檔案 I/O、socket 處理及執行緒 (threading) 的行為均與任何 POSIX 作業系統相同。然而,兩者仍有幾項重大差異。
行動平台僅能以「嵌入 (embedded)」模式使用 Python。這些平台不提供 Python REPL,也無法使用獨立的可執行檔(如 python 或 pip)。若要在行動應用程式中加入 Python 程式碼,您必須使用 Python 嵌入 API。詳情請參閱 在 Android 上使用 Python 與 在 iOS 上使用 Python。
子行程 (Subprocess):
在 Android 上雖可建立子行程 (subprocess),但 官方並不支援。具體而言,Android 不支援 System V IPC API 的任何部分,因此無法使用
multiprocessing模組。iOS 應用程式無法使用任何形式的子行程、多行程 (multiprocessing) 或行程間通訊 (IPC)。若 iOS 應用程式嘗試建立子行程,該發起動作的行程將會卡死或崩潰。除了 iOS 專用的 API 外,iOS 應用程式無法得知其他正在執行的應用程式,也無法與其進行通訊。
行動應用程式修改系統資源(如系統時鐘)的權限有限。這類資源通常 可供讀取 ,但嘗試修改時多半會失敗。
主控台 (Console) 輸入與輸出:
在 Android 上,原生的
stdout與stderr未導向至任何地方,因此 Python 會安裝自有的串流 (stream),將訊息重新導向至系統日誌。您可以分別在python.stdout與python.stderr標籤下查看這些訊息。iOS 應用程式的主控台輸出功能有限。雖然
stdout與stderr確實存在,且在 Xcode 中執行時可看見寫入其中的內容,但這些內容 不會 記錄在系統日誌中。若使用者提供應用程式日誌作為診斷輔助,其中並不會包含任何寫入stdout或stderr的細節。行動應用程式完全沒有可用的
stdin。雖然應用程式可以顯示螢幕鍵盤,但那是軟體功能,並未連接至stdin。