Python 內建的 標準函式庫(Standard Library) 是 Python 內建的模組集合,無需額外安裝即可使用。以下是 Python 3.x 內建的標準函式庫完整列表,按類別分類,避免使用時發生錯誤。
1. 基本內建函式庫
這些函式庫提供 Python 核心功能:
builtins:內建函式(如print()、len()、open())sys:與 Python 直譯器相關的功能(如sys.argv、sys.exit())os:與作業系統交互(如os.path、os.environ)time:時間相關函式(如time.sleep()、time.time())datetime:日期與時間處理(如datetime.datetime.now())calendar:提供日期計算(如calendar.month())argparse:命令列引數解析getopt:簡單命令列引數解析(類似 C 語言getopt)
2. 數學與數值計算
math:基本數學函式(如math.sqrt()、math.pi)cmath:複數數學函式(如cmath.sqrt(-1))decimal:高精度小數運算(如decimal.Decimal())fractions:分數運算(如fractions.Fraction(1, 3))random:隨機數產生(如random.randint()、random.choice())statistics:統計函式(如statistics.mean()、statistics.median())itertools:迭代工具(如itertools.combinations())functools:函式工具(如functools.lru_cache())operator:函式運算符(如operator.itemgetter())
3. 檔案與目錄操作
os.path:處理路徑(如os.path.join())shutil:檔案操作(如shutil.copyfile())pathlib:物件導向的路徑操作(如Path("file.txt").exists())glob:搜尋檔案(如glob.glob("*.py"))fnmatch:檔名匹配(如fnmatch.fnmatch("data.txt", "*.txt"))tempfile:建立臨時檔案filecmp:檔案比較send2trash(需額外安裝):將檔案移至回收桶
4. 資料壓縮與存取
zlib:壓縮與解壓縮(如zlib.compress())gzip:Gzip 壓縮(如gzip.open("file.gz"))bz2:Bzip2 壓縮lzma:LZMA 壓縮tarfile:處理.tar檔案zipfile:處理.zip檔案
5. 文本與字串處理
re:正則表達式(如re.search())string:字串函式(如string.ascii_letters)difflib:比較字串差異(如difflib.SequenceMatcher())textwrap:格式化字串(如textwrap.wrap())unicodedata:Unicode 處理stringprep:字符串準備(較少使用)readline:命令列補全(主要用於互動模式)
6. 資料結構與演算法
collections:擴展的資料結構(如collections.Counter())heapq:最小堆積(如heapq.heappush())bisect:二分搜尋(如bisect.bisect())array:高效數組(如array.array())queue:佇列(如queue.Queue())weakref:弱引用types:類型操作copy:物件複製(如copy.deepcopy())
7. 並行與多執行緒
threading:多執行緒(如threading.Thread())multiprocessing:多處理程序(如multiprocessing.Process())concurrent.futures:高層級並行(如ThreadPoolExecutor())subprocess:執行系統指令(如subprocess.run())sched:排程任務asyncio:非同步 I/O(如asyncio.run())
8. 網路與網路協議
socket:網路連線(如socket.socket())ssl:SSL/TLS 加密http.server:簡單 HTTP 伺服器urllib:HTTP 請求(如urllib.request.urlopen())requests(需額外安裝):更簡單的 HTTP 請求ftplib:FTP 連線poplib:POP3 電子郵件smtplib:SMTP 電子郵件imaplib:IMAP 電子郵件json:JSON 解析(如json.loads()、json.dumps())xml.etree.ElementTree:解析 XMLhtml.parser:解析 HTMLcgi:CGI 程式處理uuid:生成唯一 ID
9. 作業系統介面
platform:獲取系統資訊(如platform.system())resource:獲取資源使用情況ctypes:C 語言函式庫互通signal:處理系統訊號(如signal.SIGINT)psutil(需額外安裝):獲取 CPU、記憶體使用情況
10. 測試與偵錯
unittest:單元測試(如unittest.TestCase)doctest:測試 Python 內嵌說明pdb:Python 除錯器(如pdb.set_trace())traceback:處理異常追蹤warnings:發出警告訊息timeit:測試程式效能cProfile:效能分析工具
11. 安全與加密
hashlib:雜湊函式(如hashlib.md5())hmac:HMAC 驗證secrets:安全隨機數(如secrets.token_hex())
12. 其他實用模組
logging:日誌記錄(如logging.debug())configparser:讀取.ini設定檔shlex:解析 Shell 指令codecs:編碼轉換(如codecs.open())
這些函式庫都是 Python 內建標準函式庫,可直接匯入使用,不需要額外安裝。如果你需要第三方函式庫(如 numpy、pandas、requests),則需要使用 pip install 來安裝。
這份清單能幫助你避免誤用非標準函式庫,確保程式碼的兼容性與可移植性! 🚀
沒有留言:
張貼留言