How to execute Python code from within Visual Studio Code
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Ancient Construction
--
Chapters
00:00 Question
00:53 Accepted answer (Score 125)
02:31 Answer 2 (Score 284)
04:16 Answer 3 (Score 92)
05:11 Answer 4 (Score 79)
05:39 Thank you
--
Full question
https://stackoverflow.com/questions/2998...
Question links:
[Visual Studio Code]: https://code.visualstudio.com/
[downloads page]: https://code.visualstudio.com/Download
Accepted answer links:
[image]: https://i.stack.imgur.com/IbyrC.png
[image]: https://i.stack.imgur.com/4cymA.png
Answer 2 links:
[Code Runner Extension]: https://marketplace.visualstudio.com/ite...
[image]: https://i.stack.imgur.com/C05sk.gif
Answer 3 links:
[Python language extension]: https://marketplace.visualstudio.com/ite...
Answer 4 links:
[add a custom task]: https://www.stevefenton.co.uk/Content/Bl.../
--
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.


