How to execute Python code from within Visual Studio Code

We Are Going To Discuss About How to execute Python code from within Visual Studio Code. So lets Start this Python Article.

How to execute Python code from within Visual Studio Code

How to solve 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:
Install the Code Runner Extension.
Open the Python code file in Text Editor.
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
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
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" } }

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:
Install the Code Runner Extension.
Open the Python code file in Text Editor.
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
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
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" } }

Solution 1

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

Original Author Jun Han Of This Content

Solution 2

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).

Original Author python_starter Of This Content

Solution 3

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.

Original Author vlad2135 Of This Content

Solution 4

You can add a custom task to do this. Here is a basic custom task for Python.

{
    "version": "0.1.0",
    "command": "c:\\Python34\\python",
    "args": ["app.py"],
    "problemMatcher": {
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*)+s$",
            "message": 1
        }
    }
}

You add this to file tasks.json and press Ctrl + Shift + B to run it.

Original Author Fenton Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment