可以使用VSCode连接到远程的Python进程并在本地进行调试吗?

huangapple go评论94阅读模式
英文:

Can VSCode attach to a remote Python process and debug locally?

问题

我有一个由Python脚本构建的.par文件,它在远程机器上运行。我在本地机器上有源代码。我是否可以将本地的Python代码附加到在远程机器上运行的进程中,并使用断点、步骤等进行调试?

我知道Visual Studio有类似的功能用于C#程序的调试,但不确定VScode是否可以对Python脚本做同样的事情。

英文:

I have a .par file built from python scripts, and it runs on a remote machine. I have the source codes on my local machine. Is it possible for me to attach my local python codes to the process running on the remote machine, and do the debugging using break points, steps, etc?

I know Visual studio has similar features for c# program debugging, but not sure whether VScode can do the same thing for Python scripts.

答案1

得分: 1

你可以尝试使用ptvsd包

  1. 在远程机器上通过在终端中运行以下命令安装ptvsd包:pip install ptvsd
  2. 在远程机器上的Python代码中,添加以下代码以启用远程调试:
import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 5678))

这将启动ptvsd调试器,并在5678端口上监听传入的连接。

  1. 在远程机器上启动你的Python进程。

  2. 在本地机器的VS Code中,将launch.json中的"host"和"port"属性设置为远程机器的IP地址和端口。你可以参考文档了解更多细节。

英文:

You can try to use ptvsd package.

  1. Install the ptvsd package on the remote machine by running the
    following command in a terminal: pip install ptvsd
  2. In your Python code on the remote machine, add the following lines of code to enable remote debugging:
import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 5678))

This will start the ptvsd debugger and listen for incoming connections on port 5678.

  1. Start your Python process on the remote machine.

  2. In VS Code on your local machine, set the "host" and "port" properties to the IP address and port of the remote machine in the launch.json. You can refer to the document for more details.

huangapple
  • 本文由 发表于 2023年8月9日 03:30:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定