LoginSignup
9
7

More than 5 years have passed since last update.

Pythonのリモートデバッグ (Visual Studio Code編)

Last updated at Posted at 2018-04-19

VSCode使って、リモートサーバ上で動作するスクリプトに対してRemote Debugを実施する手順。
過去に ptvsd のバージョン依存などでハマったのでメモも兼ねて。

事前準備

Pythonライブラリ

ptvsd をインストールすればよいが、バージョン 3.0.0 でないと、VSCode上でうまく動作しない様子。( このissue を参照 )

pip install ptvsd==3.0.0 のように、明示的にバージョンを指定してインストールしておく。

VSCodeのTask設定

ワークスペースの launch.json に、以下のようなエントリを追加しておく。

        {
            "name": "Python: Attach",
            "type": "python",
            "request": "attach",
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/path/to/remote/workspace",
            "port": 3000,
            "secret": "xxx",
            "host": "xxx.xxx.xxx"
        },

実行対象のスクリプト

スクリプトの先頭に以下の文を仕込む。
リモートサーバ上でスクリプトを実行すると、ptvsd.wait_for_attach() の行でデバッガの接続を待機するようになる。

import ptvsd
ptvsd.enable_attach(secret='hoge', address=('0.0.0.0', 3000))
ptvsd.wait_for_attach()

デバッグの実行

Ctrl + Shift + D 又はサイドバーからデバッグ画面を開き、プルダウンメニューから launch.json で設定した構成名を選択する。

image.png

F5キーまたはデバッグ開始ボタンを押すと、launch.json で指定した接続先の待機中プロセスにattachされる。
あとは、ローカル実行時と同様に操作が可能。

9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7