0

Environment: Windows 10 x64 VSCode Version 1.23.1 python 3.6.4

I've been trying to switch from sublime text 3 to VS code. I have python 3.6.4 installed in my computer, all set with the PATH and other stuff.

In my sublime text 3, I set the build for python 3 (*.sublime-build) as:

{ 
"shell_cmd": "python --version && echo. && python -u \"$file\"",

"cmd": ["C:/Users/jxie0/AppData/Local/Programs/Python/Python36/python.exe", "-u", "$file"], 
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9])*", 
"selector": "source.python" 
}

this will add a python version output with a blank line, before running the python code, take print('hello world') as an example:

Python 3.6.4

hello world
[Finished in 0.1s]

And sublime automatic add a timer for the code at the end.

In my current task.json for VS code, I have:

{
    "version": "0.1.0",
    "command": "python",
    "isShellCommand": true,
    "args": ["${file}"],
    "showOutput": "always"
}

This will work, but just simply show the output of the python codes:

hello world

Is there anyway I can tune it into something that can give me same kind of output style as I got from sublime text? If the timer is not possible, at least can I add a shell command as a header to show the python 3 version?

Additional question: what does that "version": "0.1.0", do in VS code? Which version does it refer to?

Thanks!

jxie0755
  • 1,682
  • 1
  • 16
  • 35

1 Answers1

1

You can use the same command as you were using before, the difference is in how the file to be passed is referenced. In vscode the equivalent would be

python --version && echo && python -u ${file}

This command can be broken up into three commmands, the first, python --version, prints the python version. The second, echo, prints the empty line. The third, python -u ${file}, actually runs the python script.

As for the timer, I don't know of a way to make that occur in vscode but it would not be too difficult to implement this in the script itself, such as in Measure time elapsed in Python?


A simple configuration for this task could be as follows:

{
"version": "2.0.0",
    "tasks": [
        {
            "label": "python",
            "type": "shell",
            "command": "python --version && echo && python -u ${file}",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            }
        }
    ]
}

Note that this assumes you have python on your path, otherwise you will have to specify the path to python.exe when the python command is used in command.

fanduin
  • 1,149
  • 10
  • 17