How to execute Python code from within Visual Studio Code
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).
Running python script in Visual Studio Code; how to get `input ()` to work?
Introduction
You will need to run your script from the command-line (terminal), instead of directly in Visual Studio Code, if you would like to interact with the program as a normal user would.
> python name_of_program.py
Elaboration
The output displayed inside Visual Studio Code is not meant to be used for interacting with the underlying script, nor does it have the capability to read any input directly from your keyboard (it simply shows the output of whatever you have decided to run).
Workaround
What you could do is to edit your task-file to automatically spawn a terminal of your choosing instead of running the python
-interpreter directly.
Depending on what operating system you are on, and the terminals available, the edits required to do this might look a little different, but they should all follow the same pattern.
{
"version": "0.1.0",
"command": "urxvt",
"isShellCommand": false,
"showOutput": "always",
"args": [ "-e", "python ${file}" ]
}
N O T E
In the above,urxvt
is the name of my choice for terminal,-e
is the flag required to pass a command that is to be executed upon startup, andpython ${file}
is the command to execute.
My recommendation is to get the command necessary to fire up a new terminal, and directly execute a python script, working elsewhere before editing your task-file.
Current shortcut to run Python in VS Code
I'm using Windows so i can't give you a specific answer. But Code > Preferences > Keyboard Shortcuts, search with keyword run python file
, you will get related shortcuts.
Running Python File via shortcut in vscode
File --> Python --> Keyboard shortcuts
Search for 'Python:Run Python File in Terminal'. You can configure your own convenient keyboard shortcuts.
Visual Studio Code: How to choose cmd terminal for execution of Python files
This is a bug in v1.60.0 and it will be fixed in next release.
See bug report: Python extension does not honor default terminal type
Run Code vs Run Python File in Terminal for VSCODE
Solved it myself - the answer was simply here:
How to execute Python code from within Visual Studio Code
I needed to change the path of the code-runner in the settings.json file as that was activated.
Related Topics
"Importerror: No Module Named Site" on Windows
How to Locate Element of Credit Card Number Using Selenium Python
Function for Factorial in Python
Differencebetween 'Same' and 'Valid' Padding in Tf.Nn.Max_Pool of Tensorflow
How to Check a String for Specific Characters
Get the Second Largest Number in a List in Linear Time
Python Socket Receive Large Amount of Data
How to Read the Rgb Value of a Given Pixel in Python
Why Does Python Code Use Len() Function Instead of a Length Method
Tensorflow Only Running on 1/32 of the Training Data Provided
Replacing Few Values in a Pandas Dataframe Column with Another Value
Elegant Python Function to Convert Camelcase to Snake_Case