想用 Python 處理 XML,翻出以前的腳本卻跑不起來——少了 lxml 模組。原以為照舊 pip 一下就好,結果連續失敗幾次,最後才發現是自己敲錯了套件名稱。
我的系統是 Ubuntu 12.04,正確指令是:
sudo apt-get install python-lxml
之前輸入 sudo apt-get install lxml 一直提示找不到套件。這就帶出一個實用問題:如何根據關鍵字找出正確的套件名稱?
apt-get help 裡並沒有提供關鍵字搜尋的參數,改用 apt-cache:
apt-cache search lxml
輸出(節錄):
python-lxml - pythonic binding for the libxml2 and libxslt libraries python-lxml-dbg - ... (debug extension) python-lxml-doc - ... (documentation) python3-lxml - pythonic binding for the libxml2 and libxslt libraries python-pyquery - jQuery-like library for python
找到名字後,用 show 看細節:
apt-cache show python-lxml # 版本、安裝狀態、依賴關係 apt-cache showpkg python-lxml # 依賴與被依賴的完整資訊 apt-cache policy python-lxml # 安裝狀態與可用版本 apt-cache depends python-lxml # 此套件依賴哪些套件 apt-cache rdepends python-lxml # 反向依賴:誰需要它
apt-cache search <name>:搜尋套件,支援正則表達式。apt-cache show <name>:顯示套件資訊(版本、狀態、依賴)。apt-cache showpkg <name>:顯示依賴關係。apt-cache policy <name>:顯示安裝狀態與版本。apt-cache depends / rdepends <name>:正向/反向依賴。結論:Debian/Ubuntu 上遇到「找不到套件」時,先 apt-cache search 確認正式名稱,再 apt-cache show 看依賴,最後才 apt-get install,能省下大量瞎試的時間。