How to execute Python code from within Visual Studio Code How to execute Python code from within Visual Studio Code python python

How to execute Python code from within Visual Studio Code

There is a much easier way to run Python, and it doesn't need any configuration:

  1. Install the Code Runner Extension.
  2. Open the Python code file in Text Editor.
  3. To run Python code:
  • use shortcut Ctrl + Alt + N
  • or press F1 and then select/type Run Code,
  • or right click the Text Editor and then click Run Code in the editor context menu
  • or click the Run Code button in the editor title menu
  • or click Run Code button in the context menu of file explorer
  1. To stop the running code:
  • use shortcut Ctrl + Alt + M
  • or press F1 and then select/type Stop Code Run
  • or right click the Output Channel and then click Stop Code Run in the context menu

Run Python

If you want to add the Python path, you could go to FilePreferenceSettings, and add the Python path like below:

"code-runner.executorMap":{  "python": "\"C:\\Program Files\\Python35\\python.exe\" -u"}

In case you have installed the Python extension and manually set your interpreter already, you could configure your settings.json file as follows:

{    "python.pythonPath": "C:\\\\python36\\\\python36.exe",    "code-runner.executorMap":    {        "python": "$pythonPath -u $fullFileName"    }}

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"

Enter image description here

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;

  1. Change the Command property from "tsc" (TypeScript) to "Python"
  2. Change showOutput from "silent" to "Always"
  3. Change args (Arguments) from ["Helloworld.ts"] to ["${file}"] (filename)
  4. Delete the last property problemMatcher
  5. Save the changes made

Enter image description here

You can now open your .py file and run it nicely with the shortcut Ctrl + Shift + B (Windows) or Cmd + Shift + B (Apple).

All these answers are obsolete now.

Currently you have to:

  1. install the Python language extension (and Python, obviously)
  2. open folder (important!), open any Python file inside that folder
  3. switch to debug "tab"(?) and click on the gearbox (with hint 'Configure of Fix 'launch.json'')
  4. save the opened launch.json file (it's placed in .vscode subdirectory in the folder opened in step #2)
  5. finally, click the green triangle or hit F5

No additional extensions or manual launch.json editing is required now.