2

I have already seen this question: How to execute Python code from within Visual Studio Code

But all those answers are about tasks from old versions ("0.1.0" or "0.2.0"). VS Code is now version "2.0.0" and some parameters have changed.

I'm trying to create this task through Run Task:

tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run File",
            "command": "python ${file}",
            "problemMatcher": [],
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": true,
                "panel": "shared"
            },
            "group": {
                "kind": "build",
                "isDefault": true

            }
        }
    ]
}

But it does not work! Error message:

Terminal with ID 8 does not exist (has it already been disposed?)

What can be wrong here?


I can use Run Python File in Terminal or just enter python myFile.py in Terminal - both of them work!

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
Mikhail_Sam
  • 10,602
  • 11
  • 66
  • 102
  • I had this error once when I closed the implemented terminal. Restart vs code should help or (better solution) you update vs code to the latest version 1.19.2. (As far as I know, this error is fixed in this version) – Gmork Jan 13 '18 at 07:01

1 Answers1

3

This is my tasks.json file for python.

{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "Run Python",
        "type": "shell",
        "command": "python",
        "args": [
          "${file}"
        ],
        "problemMatcher": [],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

In case you didn't know, you can copy and paste this in your tasks.json file available in the .vscode folder. If not you can create it manually. Now you can use the shortcut Ctrl+Shift+B to run your python code automatically.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Sriganesh
  • 31
  • 3
  • For virtuelenv environments provide the path to the python.exe inside the virtuelenv environment to load it automatically, for example `"command": "scripts\\python.exe"` or `"command": "bin/python"` – Gerald Schneider Jul 17 '23 at 07:16