LoginSignup
0
3

More than 5 years have passed since last update.

[Visual Studio Code][Python] Python向け tasks.json + problemMatcher の設定

Last updated at Posted at 2017-02-26

目的

PythonのTracebackのエラーが表示されるようにしたかった

現状の問題点(2017/02/27)

  • 出力の解析が行ごとでしかできない
  • 複数行に対する解析で、ループ指定が途中行以降でしかできない(ESLint向けなど)
    • => 最終行を指定したキャプチャができない

環境

  • Visual Studio Code 1.9.1

解析対象サンプル

def main():
    raise Exception("Exception message")

if __name__ == '__main__':
    main()
Traceback (most recent call last):
  File "c:\path\to\test.py", line 5, in <module>
    main()
  File "c:\path\to\test.py", line 2, in main
    raise Exception("Exception message")
Exception: Exception message

設定例

Python向けtasks.json(拡張子はJSONだけどコメントも書ける)
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "python",
    "isShellCommand": true,
    "args": [
        "${file}"
    ],
    "showOutput": "always",
    "problemMatcher": {
        "owner": "python",
        "fileLocation": "absolute",
        "pattern": //[
            {
                "regexp": "^.*File \"(.*)\", line (\\d+?), in.*$",
                "file": 1,
                "line": 2
                //"loop": true
            }
            // ホントは最終行のエラーメッセージを取りたいけども
            // 現状の"problemMatcher"の構文では対応できない
            // https://code.visualstudio.com/Docs/editor/tasks#_defining-a-multiline-problem-matcher
            // https://github.com/Microsoft/vscode/blob/master/src/vs/platform/markers/common/problemMatcher.ts
            //{
            //    "regexp": "^(.*):(.*)$",
            //    "severity": 1,
            //    "message": 2
            //},
        //]
    }
}

出力例としては以下のようになります。

1.png

補足

実行時に例外が投げられた場所で止めたいということであれば、デバッグ画面の左下のブレークポイントで「All Exceptions」にチェックをいれるだけでも解決すると思います。(ただし、F5を2回押さないといけないので1手順多い)

1.png

参考

0
3
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
0
3