VS CodeでPythonのビルドタスクを実行するとエラーが出て実行ができない問題

前回はデバッグ実行ができないという問題がありました。
VS CodeでPythonをデバッグ実行するとエラーが出て実行ができない問題 - EF Blog

今回はCtrl + Shift + Bで実行できるビルドタスクのエラーです。

VS Codeのバージョンは1.14.2
Pythonのバージョンは3.6.0です。

エラー文は

ビルド タスクが見つかりません。定義するには 'Configure Build Task' (ビルド タスクを構成します) を押してください。

と出ていました。


その前に、エラーが出る前の設定までの流れを記しておきます。

  • test.pyを作成
# test.py
print("Hello world")
  • Ctrl+Shift+Bで実行

ビルド タスクが見つかりません。定義するには 'Configure Build Task' (ビルド タスクを構成します) を押してください。
とエラーが出たので押した。

「Others 任意の外部コマンドを実行する例」
を押した。

  • tasks.jsonが生成される

デフォルトがこうなので

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "echo",
            "command": "echo Hello",
            "type": "shell"
        }
    ]
}

他のサイト参考に

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "Python",
            "command": "python",
            "type": "shell"
        }
    ]
}

こう書き直した。

  • 再びCtrl+Shift+Bで実行

ビルド タスクが見つかりません。定義するには 'Configure Build Task' (ビルド タスクを構成します) を押してください。
とエラーが出る。

という経緯をたどりました。


どういう設定をすればいいのかわからないので
VSCode tasks.json
ググるとなんか出てきました。
code.visualstudio.com

見てみるとTypeScriptしか書かれていないのでPythonCustom tasksの方なんでしょうね。
タスクのプロパティは

  • taskName:タスク名
  • type:タスクのタイプ。shellとprocessの2種類がある
  • command:実行するコマンド
  • windows:任意のWindows固有のプロパティ
  • group:タスクが属するグループの設定
  • presentation:タスク出力の設定

の6つっぽいです。

taskNameとtypeとcommandはすでに書いてあるのでそのままにして、
groupを追加してみたいと思います。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "Python",
            "command": "python",
            "type": "process",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

groupのkindとisDefaultはgroupを補完候補を選択したときに出たデフォルトの設定だったのでこのままにします。

保存してとりあえずこれで実行してみます。

> Executing task: C:\Users\EF\AppData\Local\Programs\Python\Python36\python.exe  <

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

実行に成功はしましたが、対話モードになっており、開いているファイルを実行していません。

そのまま読み進めているとargsというものが出てきました。
たぶん引数が設定できるようなので書いてみます。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "Python",
            "command": "python",
            "type": "process",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "args": [
                "${file}"
            ]
        }
    ]
}

argsを追加しました。
"${file}"は現在アクティブなファイルのパスを取ってきてくれるようです。
これで実行してみます。

> Executing task: C:\Users\EF\AppData\Local\Programs\Python\Python36\python.exe e:\pg\Python\Chainer\test.py <

hello world

端末はタスクで再利用されます、閉じるには任意のキーを押してください。

おお!ついに実行できました!!!
必要最低限これだけの設定をすればビルドタスクが実行できるようです。

これで当初の目標は達成できました。


余談ですが、そのまま読み進めると重要なことが下の方に書いてありました。

Convert from "0.1.0" to "2.0.0"
Since the 2.0.0 version comes with lots of new auto-detection features, you can try removing an existing tasks.json file to see which tasks still work. Simply rename the existing tasks.json to tasks.json.off. If you have lots of customizations then you can switch by changing the version attribute to "2.0.0". After doing so, you might encounter warnings because some old properties are now deprecated. Here is how to get rid of the deprecations:

  • isShellCommand: Use the "type": "shell" property instead.
  • isBuildCommand: Use the "group": "build" property instead.
  • isTestCommand: Use the "group": "test" property instead.
  • echoCommand: Use the "presentation" : { "echo": "..." } property instead.
  • showOutput: Use the "presentation" : { "reveal": "..." } property instead.
  • suppressTaskName: By default, the task name gets appended to the list of arguments when running a task version 0.1.0. Since version 2.0.0 supports commands per task, simply inline the command into the task and specify the arguments accordingly. Consider the following 0.1.0 configuration:

簡潔に言うと"0.1.0から2.0.0になってから非推奨のプロパティがあるから新しいのに変えような"という感じですね。

このサイトはtasks.jsonのバージョンを0.1.0で説明しているので、自分のtasks.jsonにコピペすると警告が出たり、エラーを言われたりして何じゃこりゃってなりました。
これを最初のほうに持ってくれば読みやすかったのに...。


そんなこんなでとりあえずは最低限の開発環境が整えられました。