Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute Python code from within Visual Studio Code

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?

like image 743
RPiAwesomeness Avatar asked May 01 '15 13:05

RPiAwesomeness


People also ask

How do I run a Python script in Visual Studio code?

Just click the Run Python File in Terminal play button in the top-right side of the editor. Select one or more lines, then press Shift+Enter or right-click and select Run Selection/Line in Python Terminal. This command is convenient for testing just a part of a file.

Can you execute code in Visual Studio code?

Build and run your code in Visual Studio To build your project, choose Build Solution from the Build menu. The Output window shows the results of the build process. To run the code, on the menu bar, choose Debug, Start without debugging. A console window opens and then runs your app.


1 Answers

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"     } } 
like image 112
Jun Han Avatar answered Sep 23 '22 07:09

Jun Han