如何在PyCharm中进行远程调试

时间:2016-03-02 10:30:05

标签: python debugging ssh pycharm

我现在面临的问题:

  • 我通过SSH在远程主机上部署Python代码
  • 脚本传递一些参数,必须由特定用户运行
  • 我创建的PyCharm run / debug配置通过SSH通过其他用户连接(无法与实际运行脚本的用户连接)
  • 我想通过PyCharm远程调试此代码...我设法完成所有配置,我只是得到权限错误。

有没有办法如何以特定用户的身份运行/调试脚本(比如sudo su - user)?

我已经阅读过如何在PyCharm的远程/调试配置中指定一些Python Interpeter选项,但没有设法获得可行的解决方案。

2 个答案:

答案 0 :(得分:8)

If you want an easy and more flexible way to get into the PyCharm debugger, rather than necessarily having a one-click "play" button in PyCharm, you can use the debug server functionality. I've used this in situations where running some Python code isn't as simple as running python ....

See the Remote debug with a Python Debug Server docs for more details, but here's a rough summary of how it works:

  1. Upload & install remote debugging helper egg on your server (On OSX, these are found under /Applications/PyCharm.app/Contents/debug-eggs)
  2. Setup remote debug server run configuration: click on the drop-down run configuration menu, select Edit configurations..., hit the + button, choose Python remote debug.
    • The details entered here (somewhat confusingly) tell the remote server running the Python script how to connect to your laptop's PyCharm instance.
    • set Local host name to your laptop's IP address
    • set port to any free port that you can use on your laptop (e.g. 8888)
  3. Now follow the remaining instructions in that dialog box: copy-paste the import and pydevd.settrace(...) statements into your code, specifically where you want your code to "hit a breakpoint". This is basically the PyCharm equivalent of import pdb; pdb.set_trace(). Make sure the changed code is sync'ed to your server.
  4. Hit the bug button (next to play; this starts the PyCharm debug server), and run your Python script just like you'd normally do, under whatever user, environment etc. When the breakpoint is hit, PyCharm should drop into debug mode.

答案 1 :(得分:0)

我终于(最终)使用ssh RemoteForward打开了它,就像这样:

ssh -R 5678:localhost:5678 user@<remotehost>

然后在此ssh会话中启动脚本。 python脚本主机必须连接到localhost:5678,当然您的本地pycharm调试器必须侦听5678 (或您选择的任何端口)

相关问题