Configure Vs code version 2.0.0 Build Task for python

11,693

Solution 1

In VS Code go Tasks -> Configure Tasks

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "Run File",
            "command": "python ${file}",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "always",
                "panel": "new",
                "focus": true
            }
        },
        {
            "taskName": "nosetest",
            "command": "nosetests -v",
            "type": "shell",
            "group": {
                "kind": "test",
                "isDefault": true
            },
            "presentation": {
                "reveal": "always",
                "panel": "new",
                "focus": true
            }
        }
    ]
}

command: runs current python file

group: 'build'

presentation:

  • always shows the shell when run
  • uses a new shell
  • focuses the shell (i.e. keyboard is captured in shell window)

2nd Task is configured as the default test and just runs nosetest -v in the folder that's currently open in VS Code.

The "Run Build Task" (the one that's bound to Ctrl+Shift+B) is the one that's configured as the default build task, task 1 in this example (see the group entry).

EDIT:
Suggested by @RafaelZayas in the comments (this will use the Python interpreter that's specified in VS Code's settings rather than the system default; see his comment for more info):

"command": "${config:python.pythonPath} ${file}"

Solution 2

Here is my configuration for the build (Ctrl+Shift+B)

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "python",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "args": [
                "setup.py",
                "install"
            ],
            "presentation": {
                "echo": true,
                "panel": "shared",
                "focus": true
            }
        }
    ]
}

Solution 3

...Don't have enough reputation to comment on the accepted answer...

At least in my environment (Ubuntu 18.04, w/ virtual env) if arguments are being passed in with "args", the file must be the first argument, as @VladBezden is doing, and not part of the command as @orangeInk is doing. Otherwise I get the message "No such file or directory".

Specifically, the answer @VladBezden has does work for me, where the following does not.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "${config:python.pythonPath} setup.py", // WRONG, move setup.py to args
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "args": [
                "install"
            ],
            "presentation": {
                "echo": true,
                "panel": "shared",
                "focus": true
            }
        }
    ]
}

This took me a while to figure out, so I thought I would share.

Share:
11,693
Tungata
Author by

Tungata

Here to help and gain some software development experience.

Updated on June 04, 2022

Comments

  • Tungata
    Tungata almost 2 years

    I need help in configuring my Vs code to run scripts in python using Cntrl Shift B, I was working fine until Vs code upgraded to version 2.0.0 now it wants me to configure the Build. And I am clueless what Build is all about.

    In the past it worked well when I only needed to configure the task runner. There are youtube videos for the task runner. I cant seem to lay my finger on what the Build is all about.