使用VS Code對遠端server的python程式進行debug

陳竹軒
2 min readMar 26, 2019

--

環境

本地端:windows 10
遠端: Ubuntu 16.04 (防火牆要開一個port,此例為3000)

1.將你的.py檔上傳至遠端server上

2.兩端的python環境都pip install ptvsd

3.這邊有兩種選擇

3.1 修該原始碼法

3.1.1 在遠端的.py檔開頭加入並用一般的python啟動

import ptvsd
ptvsd.enable_attach(address=('0.0.0.0', 3000), redirect_output=True)
ptvsd.wait_for_attach()

3.1.2 本地端的.py檔不加,但是開頭要空三行或加入那三行的註解

3.2或是直接用ptvsd啟動

python3 -m ptvsd --host 0.0.0.0 --port 3000 --wait test.py

4.本地端的VSCODE點選選單中的Debug-Add Configuration

5.會有自動提示讓你選,選python: Remote Attach

5.1 port改3000

5.2 host改遠端機器所在的ip

5.3 localRoot 改"${workspaceFolder}"

5.4 remoteRoote改"."

6. 設置一個斷點

7. 以剛剛的設定啟動debug

7.1(Debug頁籤-左上角的下拉示選單)

7.2 Attach Remote Debug

7.3 啟動

Ref: https://code.visualstudio.com/docs/python/debugging#_remote-debugging

--

--