name: lsp-python description: "Python code quality checking and LSP integration using pylsp. Provides code diagnostics, completion, hover tips, and style analysis. Use when: checking Python errors/warnings, getting code completions, viewing function signatures, analyzing code quality, or fixing style issues."
使用 Python Language Server Protocol (LSP) 進行程式碼質量檢查和智慧分析。
# 單個檔案
python3 scripts/lsp-service.py check <檔案路徑>
# 批次檢查 (推薦)
python3 scripts/check_python.py <檔案或目錄>
# 批次檢查並自動修復
python3 scripts/check_python.py --auto-fix <檔案或目錄>
示例:
python3 scripts/lsp-service.py check my_script.py
python3 scripts/check_python.py src/
python3 scripts/check_python.py --auto-fix src/
python3 scripts/lsp-service.py complete <檔案> <行號> <字元位置>
python3 scripts/lsp-service.py info <檔案> <行號> <字元位置>
pip install python-lsp-serverpip install python-lsp-server[all] - 完整外掛集pip install pylsp-mypy - 型別檢查pip install pylsp-black - black 格式化檢查 Python 檔案中的錯誤和警告:
輸出示例:
⚠️ 第 3 行 [pyflakes]: 'os' imported but unused
⚠️ 第 6 行 [pycodestyle]: E302 expected 2 blank lines, found 1
✅ 沒有發現問題
獲取指定位置的程式碼補全建議:
python3 scripts/lsp-service.py complete script.py 5 10
輸出:
補全建議:
• json (模組)
• jsonpatch (模組)
• requests (模組)
檢視函式簽名、文件字串等資訊:
python3 scripts/lsp-service.py info script.py 10 5
查詢符號的定義位置:
python3 scripts/lsp-service.py goto script.py 15 10
pip install autoflake
autoflake --remove-all-unused-imports --in-place --recursive .
pip install black
black .
# 1. 備份
cp -r project/ project.backup
# 2. 清理匯入
autoflake --remove-all-unused-imports --in-place --recursive project/
# 3. 格式化
black project/
# 4. 驗證
python3 scripts/lsp-service.py check project/main.py
| 級別 | 程式碼 | 含義 |
|---|---|---|
| ❌ | 1 | Error (錯誤) |
| ⚠️ | 2 | Warning (警告) |
| ℹ️ | 3 | Information (資訊) |
| 💡 | 4 | Hint (提示) |
| 程式碼 | 含義 | 修復方法 |
|---|---|---|
| E402 | 匯入不在檔案頂部 | 移動匯入到檔案開頭 |
| E501 | 行太長 (>79 字元) | 拆分長行或使用括號 |
| W293 | 空行包含空白字元 | 刪除行尾空格 |
| E302 | 缺少空行 | 函式/類定義前加 2 個空行 |
| E712 | 布林比較風格 | if x is True → if x |
exec: python3 /path/to/lsp-python/scripts/lsp-service.py check <file>
# 檢查所有 Python 檔案
find . -name "*.py" -exec python3 scripts/lsp-service.py check {} \;
# 僅顯示有問題的檔案
for f in $(find . -name "*.py"); do
result=$(python3 scripts/lsp-service.py check "$f" 2>&1)
if ! echo "$result" | grep -q "✅ 沒有發現問題"; then
echo "=== $f ==="
echo "$result"
fi
done
references/lsp-protocol.mdreferences/pylsp-config.mdreferences/pep8-guide.md來源於7w4.net。
# 檢查安裝
which pylsp
pylsp --version
# 重新安裝
pip install --upgrade python-lsp-server
增加指令碼中的 LSP_TIMEOUT 值 (預設 10 秒)。
確保檔案使用 UTF-8 編碼,指令碼已設定 ensure_ascii=False。
這個 Skill 質量不錯,功能豐富實用。它能檢查 Python 程式碼錯誤、提供程式碼補全和提示,還支援批次檢查和自動修復。文件寫得清楚詳細,小白也能看懂。主要缺點是批次檢查指令碼有時會出問題找不到檔案,功能依賴外部工具,安裝不全可能用不了。總體來說是個靠譜好用的工具,但使用前需要確保裝好所有依賴。