How to execute Python code from within Visual Studio Code

RPiAwesomeness picture RPiAwesomeness · May 1, 2015 · Viewed 457k times · Source

Visual Studio Code was recently released and I liked the look of it and the features it offered, so I figured I would give it a go.

I downloaded the application from the downloads page, fired it up, messed around a bit with some of the features ... and then realized I had no idea how to actually execute any of my Python code!

I really like the look and feel/usability/features of Visual Studio Code, but I can't seem to find out how to run my Python code, a real killer because that's what I program primarily in.

Is there is a way to execute Python code in Visual Studio Code?

Answer

Jun Han picture Jun Han · Aug 17, 2016

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"
    }
}