PyCharm 的一项重要功能是它允许其调试器附加到本地(和 IDE 外部)运行的 python 进程。
当我尝试转移到 VSCode 以在 Python 中工作时,我正在努力配置 launch.json 以模拟 PyCharm 的附加到本地进程功能。
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}",
"port": 8001,
"secret": "my_secret",
"host": "localhost"
},
此配置是在我为调试器选择 Python: attach 选项时默认创建的,但我确信这是用于远程调试(使用端口和所有),并且大多数 Google 搜索结果只是谈论远程使用 VSCode 调试 Python。
有人成功地将一两个本地调试器附加到本地运行的多个 python 进程吗?
最佳答案
自从上次发布答案以来,VSCode 似乎已经添加了此功能。
在docs在“基本调试”下,他们解释了这个过程:
The simplest way to begin debugging a Python file is to use the Run view and click the Run and Debug button. When no configuration has been previously set, you will be presented with a list of debugging options. Select the appropriate option to quickly begin debugging your code.
Two common options are to use the Python File configuration to run the currently open Python file or to use the Attach using Process ID configuration to attach the debugger to a process that is already running.
所以在您的情况下,您可以选择左侧的“运行”选项卡,然后选择“运行和调试”。系统将提示您“选择调试配置”。由于您正在尝试附加到现有/已经运行的脚本,请选择“使用进程 ID 附加”。然后选择您要附加到的 Python 线程。
我刚刚在我的机器上尝试了这个(VS 代码版本 1.45.0 和 Python 扩展版本 2020.4.76186)并且能够附加到正在运行的进程。
关于python - VSCode 调试器附加到本地进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49819908/