Systematic Python debugging workflow for spreadsheet tasks to isolate environment issues from script logic
复制下面这句话,粘贴给 Claude Code、Codex、Cursor 等 AI 编程工具,它会读取安装说明并在你确认后完成安装。
请阅读 https://ai.atlankj.com/install/asset/gh-python-spreadsheet-debug-739921d0b250 ,按照其中的说明把「python-spreadsheet-debug」安装到你(当前 AI 工具)中。执行前先告诉我将运行的命令和写入的位置,等我确认。
查看 AI 将读取的安装说明正在读取 GitHub 原文…
内容来自 GitHub 原始文件,由原作者维护。在 GitHub 查看
When executing Python scripts for spreadsheet/data processing tasks, use this systematic debugging approach to efficiently isolate environment configuration issues from script logic errors, especially when run_shell returns opaque errors.
First, confirm the Python interpreter path and version to ensure you're working in the expected environment:
which python python3
python --version
python3 --version
This identifies whether Python is available and which version is being used.
Before running your full script, verify that required libraries can be imported successfully. Test each critical import individually:
python -c "import pandas; print('pandas:', pandas.__version__)"
python -c "import openpyxl; print('openpyxl:', openpyxl.__version__)"
python -c "import xlrd; print('xlrd:', xlrd.__version__)"
Replace library names based on your script's requirements. This identifies missing dependencies or version conflicts early.
Create and execute a minimal script that exercises only the core functionality without full business logic:
# test_minimal.py
import pandas as pd
# Test 1: Can we read a file?
try:
df = pd.read_excel('sample.xlsx')
print(f"✓ File read successful: {len(df)} rows")
except Exception as e:
print(f"✗ File read failed: {e}")
# Test 2: Can we perform basic operations?
try:
result = df.groupby('category')['amount'].sum()
print(f"✓ GroupBy operation successful")
except Exception as e:
print(f"✗ Operation failed: {e}")
Run this with: python test_minimal.py
Purpose: This isolates whether the issue is with file access, library functionality, or specific script logic.
Once the minimal test passes, run the complete script:
python your_script.py
If errors occur now, you know the environment is correctly configured and can focus on debugging the specific logic.
For rapid diagnosis, run all checks in sequence:
echo "=== Python Version ===" && python --version && \
echo "=== Key Imports ===" && python -c "import pandas, openpyxl; print('OK')" && \
echo "=== Ready for full script ==="
| Symptom | Likely Cause | Resolution |
|---|---|---|
ModuleNotFoundError | Missing package | pip install <package-name> |
ImportError with version | Version conflict | Check pip list, reinstall specific version |
FileNotFoundError | Wrong path/context | Verify working directory with pwd |
PermissionError | File access | Check file permissions or path |
run_shell returns cryptic or truncated error messages