How to execute Python code from within Visual Studio Code
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Popsicle Puzzles
--
Chapters
00:00 How To Execute Python Code From Within Visual Studio Code
00:37 Answer 1 Score 79
00:59 Accepted Answer Score 127
02:06 Answer 3 Score 99
02:46 Answer 4 Score 27
02:56 Thank you
--
Full question
https://stackoverflow.com/questions/2998...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #visualstudiocode
#avk47
ACCEPTED ANSWER
Score 127
Here is how to configure Task Runner in Visual Studio Code to run a .py file.
In your console, press Ctrl + Shift + P (Windows) or Cmd + Shift + P (Apple). This brings up a search box where you search for "Configure Task Runner"
If this is the first time you open the "Task: Configure Task Runner", you need to select "other" at the bottom of the next selection list.
This will bring up the properties which you can then change to suit your preference. In this case you want to change the following properties;
- Change the Command property from 
"tsc"(TypeScript) to"Python" - Change showOutput from 
"silent"to"Always" - Change 
args(Arguments) from["Helloworld.ts"]to["${file}"](filename) - Delete the last property 
problemMatcher - Save the changes made
 
You can now open your .py file and run it nicely with the shortcut Ctrl + Shift + B (Windows) or Cmd + Shift + B (Apple).
ANSWER 2
Score 99
All these answers are obsolete now.
Currently you have to:
- install the Python language extension (and Python, obviously)
 - open folder (important!), open any Python file inside that folder
 - switch to debug "tab"(?) and click on the gearbox (with hint 'Configure of Fix 'launch.json'')
 - save the opened launch.json file (it's placed in .vscode subdirectory in the folder opened in step #2)
 - finally, click the green triangle or hit F5
 
No additional extensions or manual launch.json editing is required now.
ANSWER 3
Score 79
You can add a custom task to do this. Here is a basic custom task for Python, add this to file tasks.json and press Ctrl + Shift + B to run it.
{
  "version": "0.1.0",
  "command": "c:\\Python34\\python",
  "args": [
    "app.py"
  ],
  "problemMatcher": {
    "fileLocation": [
      "relative",
      "${workspaceRoot}"
    ],
    "pattern": {
      "regexp": "^(.*)+s$",
      "message": 1
    }
  }
}
ANSWER 4
Score 27
There is a Run Python File in Terminal command available in the Python for Visual Studio Code extension.


