在 Debian 系统上配置 Python 调试环境,通常涉及调试工具、IDE/编辑器、调试库以及常见调试方式的配置。下面按常用场景给你一个完整、实用的配置指南。
python3 --version
如果没有或版本较低:
sudo apt update
sudo apt install python3 python3-pip python3-venv
sudo apt install python3-dbg gdb
python3-dbg 提供带调试符号的 Python(适合底层调试)。
import pdb
pdb.set_trace()
或使用 Python 3.7+:
breakpoint()
python3 test.py
| 命令 | 作用 |
|---|---|
| n | 下一行 |
| s | 进入函数 |
| c | 继续执行 |
| l | 查看代码 |
| p 变量 | 打印变量 |
| q | 退出 |
sudo apt install code
或通过官网下载 .deb
VS Code → Extensions → 搜索 Python(Microsoft)
Ctrl + Shift + D → 创建 launch.json
示例配置:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
点击 ▶ 即可调试(支持断点、变量查看、单步执行)
sudo snap install pycharm-community --classic
或下载官网版本
Settings → Python Interpreter → 选择 python3
PyCharm 自带:
适合服务器、Docker、远程 Debian 系统。
pip install debugpy
import debugpy
debugpy.listen(("0.0.0.0", 5678))
debugpy.wait_for_client()
{
"type": "python",
"request": "attach",
"connect": {
"host": "远程IP",
"port": 5678
}
}
gdb python3
run test.py
bt
或使用:
python3-dbg test.py
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
VS Code / PyCharm 选择该虚拟环境即可。
| 场景 | 建议 |
|---|---|
| 简单脚本 | pdb |
| 日常开发 | VS Code |
| 大型项目 | PyCharm |
| 服务器 / Docker | debugpy |
| C 扩展 / 崩溃 | gdb + python3-dbg |
如果你愿意,可以告诉我:
我可以给你一步到位的具体配置方案。