python - VSCode 调试器附加到本地进程

标签 python visual-studio-code attach-to-process

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/

相关文章:

python - 确认浏览器预取导致删除

python - matplotlib:如何控制所有绘图的自定义网格线型?

c++ - 在 Windows 上设置编译器路径

python-3.x - Visual Studio Code Python linting 不适用于 venv 和 wsl

c# - 调试 Visual Studio IDE 中不会发生的 .NET 崩溃

visual-studio-2010 - 如何判断我正在调试哪个进程(附加到多个进程)?

javascript - 当我不是node.exe的调用者时,如何在Windows上的nodejs中调试javascript

python - 如何在 Windows 10 上使用 Cx_Freeze 制作 exe,包括 PyQt5 和 OpenCV

python - 使用类型提示将 Python 翻译成 Cython

javascript - 如何设置VSCode将花括号放在新行上?