How do I open the interactive shell/REPL in Visual Studio Code? - python

I'm using Visual Studio Code, and I'd like to have a REPL that I can run single Python instructions in. I've found some year-old Microsoft documentation on this that says select the View > Other Windows > Interactive menu item, but there is no Other Windows item in my version of VS Code (1.44.2).
Does VS Code have a REPL feature, or do I have to run python.exe directly from the Terminal pane?

You are looking at the wrong documentation. That's for Visual Studio, not Visual Studio Code. They are annoyingly similarly named, which can be confusing, but they are two completely different IDEs.
The correct docs for VS Code is here:
https://code.visualstudio.com/docs/languages/python
There is a section for running Python codes on the Terminal:
https://code.visualstudio.com/docs/languages/python#_run-python-code
You can use the standard Python console.
Install the Python extension for VS Code
Setup and select the correct Python environment
Open the Command Palette then "Python: Select Interpreter"
Next, highlight the line you want to run, then right-click
Select the Run Selection/Line in Python Terminal
It should open up a Terminal panel and run your code in Python console
You can then keep right-clicking > Run and it will execute on the same session
There's also an option to run the entire file.
See the Run Python Code section of the docs for more info.

You can open the command palette and enter python: Start REPL
This will open the REPL.

Related

Why is VS Code showing the path to the extension and the exe when running the code? And how to make it shorter?

I'm trying to create a Python file and run the code
print ('hello there')
using Visual Studio Code IDE. However when I run this code, the terminal screen shows:
PS D:\Learning Python> & 'C:\Users\ADMIN\AppData\Local\Programs\Python\Python310\python.exe' 'c:\Users\ADMIN\.vscode\extensions\ms-python.python-2022.8.0\pythonFiles\lib\python\debugpy\launcher' '64817' '--' 'd:\Learning
Python\learn_python.py'
Hello there
Could you please explain why is it showing the path of the extension and the exe? And how to make it shorter to
PS D:\Learning Python>" (the folder stored python file
Terribly sorry. this is how python extensions work. Because the VS Code terminal integrates the powershell or cmd terminal from the computer, it need to specify the Python path to use and the path to execute the script for.
If you don't like it, you can install the Code Runner extension. Once installed, the execute code icon in the upper right corner of VS Code will have three options.
Select Run Code, the code result will be output in the OUTPUT window (only the result, no extra lines).
Select Run Python File, and the code results are output in the TERMINAL window as before.
The third Debug Python File is used as debug code.
For better use, it is recommended to add the following code to your settings.json file after installing the Code Runner extension:
"code-runner.clearPreviousOutput": true,
"code-runner.showExecutionMessage": false,

CodeRunner Setup in VSC for Python

I've installed VSC and added to it the C/C++ and CodeRunner extensions, and it all worked perfectly. I've set CodeRunner to run in Terminal and I haven't done much else to the settings.
However, I installed the Python extension now, and the CodeRunner 'Run' button displays 2 options when I click it: Run Code and Run Python File in Terminal. When I was running a C program previously, one click on 'Run' would do it.
Is there any way to eliminate the extra options when running Python code? Essentially, I just want the program to execute with one click on 'Run'. Seems like the code executes regardless of the option I choose. (See the attached picture). Thank you.
Reason:
In VS Code, the green run button provided by the "Python" extension is to execute the command "Run Python File in Terminal". It only requires us to click once to run python files in the Terminal of VS Code:
When we also use the extension "Code Runner", the run button ("Run Code") provided by it defaults to output results in "OUTPUT". In the previous use, this button will cover the green button provided by the "python" extension, but now that they are integrated, we can choose the execution method.
For setting "code-runner.runInTerminal": false, it determines whether the "code runner" runs in the "Terminal", the commands they execute are different:
"Run Code":python -u "/python_file.py"
"Run Python File in Terminal": /python.exe /python_file.py
Solution: You could use F5 to debug the code or try to close the "Code Runner" extension. (Click "Enable" to restore the use of the extension.)
Reference: Run Python File in VS Code.

Code Runner Icon not showing in Visual studio code

Today i opened my vscode editor and i saw that the code runner icon had disappeared despite that it was installed. I could only see the default run code option. Please help me solve it, I also tried uninstalling and installing the extension again and also did the same with vscode but the problem still persists![the run option os of default vscode not coderunner][1]
Go to the extensions and then search for code runner after this install it and then it will appear.
This button does not disappear, but is integrated with the original "Run Python file in Terminal" button in the Python file. Click this button and it will display two run options: "Run Code" provided by Code Runner extension and "Run Python file in Terminal" provided by Python extension.
Check the pyhon ... program language version and select the correct one if you are using several different versions.

How do I find/excute Python Interactive Mode in Visual Studio Code?

I'm unable to find/execute the interactive mode in visual studio code - could you guys step-by-step me? Seems like a ridiculous question, I guess, but none of the waypoints I Googled seem to work;
"View > Other Windows > Interactive menu commands)" https://learn.microsoft.com/en-us/visualstudio/python/python-interactive-repl-in-visual-studio?view=vs-2019
I don't have "other windows"
If there's something I need to install, please let me know.
(Yes, I'm a beginner).
In VSCode, you can enter the Python interactive window in the following places:
Option 1
F1 -> Python: Create Python Interactive Window
This is the Python interactive window that comes with VSCode.
You can enter the interactive window without inputting instructions.
Please use "Ctrl+Enter" to execute the code:
Option 2
Ctrl + Shift + `
This is the cmd window that comes with the VSCode integration computer.
We need to enter the command "python" to enter the Python interactive window:
PS
For more information about using Python in Visual Studio Code, you can refer to the official VSCode documentation: Python in VSCode.
If you have the Python extension, you can use the Python Interactive feature (this is a IPython/Jupyter console, which can run parts of your code as 'cells', i.e. snippets of code executed in one go).
This article writes about the interactive window and many more possibilities of using Jupyter in VS Code: https://code.visualstudio.com/docs/python/jupyter-support-py
Best option
When editing Python code, select something and press Shift + Enter. This will open the interactive window and run your selection as a single cell.
I have personally been annoyed many times by accidentally pressing Shift + Enter instead of Enter on a line of code which (alone) would be a syntax error. Then, one day I decided to start using this feature consciously.
Terminal fallback
Press:
Ctrl+`
This opens a system shell (bash, or on Windows: Powershell or cmd).
Run python, or ipython if you have it (pip install ipython), and use the interactive window. In the terminal, you can also execute your script, by typing python myscript.py arguments.
If you type # %% in your vscode editor while editing a .py file,
then a interactive cell is created and it can be evaluated. The nice part of this is that # denotes comment in a .py file so you can save and run your code as a script

Visual Studio Code is not showing the ouput of Python

I am really new to Python and just started the course "Python 3 basics by Sendtex (Link: https://www.youtube.com/channel/UCfzlCWGWYyIQ0aLC5w48gBQ)".
I am trying to use Python with Visual Studio Code.
The downloaded version of Python is 3.6.4.
I have installed the Python extension in Visual Studio Code and have followed the procedure.
However, I am not able to get any output. Below is the attached screenshot.
If I select Python it enters debugging mode. So, there isn't any output.
Using the Python Extension Run/Debug commands
Notes
I recommend reviewing the official Visual Studio Code Python documentation and tutorial. That provides several of the basics, with running and debugging the "official" way.
There are multiple ways to configure built-in launches:
When running or debugging Python in VS Code, there are a few options that you can change in your "launch.json" file. See this answer for more details.
Run or Debug with the built-in Python extension commands
You can run or debug using the "triangle" icon and dropdown in the upper right of your editor window, when the editor window is focused on a python script file (i.e. typically a file named *.py).
You may need to setup a run/debug configuration to use this (see below), or it may automatically step you through creating one.
The output will normally go to a new "Python" terminal in the "terminal" window (Ctrl-`, to open it). See this answer for how to change that behavior.
Setting up a run/debug configuration
Easy option:
Use the menu item **Run/Add Configuration, and follow the prompts at the top of the VS Code window.
Some of the additional details are shown below.
More involved option:
Click the run/debug icon on the left vertical icon toolbar.
The window says "RUN AND DEBUG" at the top, with a drop-down that shows "No Configurations" for a given folder or workspace until you set this up.
The gear to the right of the dropdown will have a red dot if you need to configure this:
Updated Image (1/2022) - Changed from just 'Debug' to 'Run and Debug':
Click the gear icon (or in the menu, use Run/Open Configurations), and in the middle at the top, Visual Studio Code will open a window titled "Select a debug configuration".
The available options will depend on exactly how you have Visual Studio Code setup and configured
There should at least be a "Python File" option with the Python extension installed. Select it.
Now your debug configuration box on the top left of the debug pane should say "Python: Current File":
Visual Studio Code will add a file to your current workspace or working folder called .vscode/launch.json, and opened it for edit.
During installation if you forget to tick in check box for path and then if you are running any python file on visual studio it will not return any output untill and unless you have not added it into visual studio code.
First of all go to File → Preference → Settings.json
"code-runner.executorMap":
{
"python": "\"C:\\Program Files\\Python39\\python.exe\""
I have attached the screenshot for your better understanding!
Simplest Answer: Run the file directly in a terminal
Open a terminal in VS Code (View/Terminal in menu or Ctrl-`, which is the "Ctrl" key and the "Grave" or "Tick-mark" key, normally at the top left of keyboard above tab).
This will open the terminal to the root of the folder VS Code has open (the one that shows up in the Explorer sidebar - Ctrl-Shift-E to see).
Run the python script in the current directory with a simple python my_script.py command.
If the script is in another directory than the root of the current workspace or folder you opened the terminal in, you will need to change to that directory (using cd commands, etc.), or add the absolute or relative folder path to the name of the script.
The output will show up in the same terminal.
Notes
You must have a python executable on your path, be in an active virtual environment, or give the full path to the python executable.
Your python command may be python2 or python3 on some installs (usually linux), and can be just py for the Python Launcher (auto-installs with typical Python for Windows installs).
This option will not use any 'launch.json' configurations, and cannot be easily used to debug with.
You can configure the default terminal that is used, such as Git Bash, CMD, or Powershell (on Windows). You can also open additional terminal windows of any configured type using the "+" icon and it's dropdown, in the terminal window.
I found this post when trying to find how to show my python code output in the VSCode output window instead of the integrated terminal window. In my case the problem was caused by a Code-runner setting. Here's how I fixed my problem :
Open Command Palette(CTRL + SHIFT + P).
Search and select Preferences : Open Settings (UI).
Search the following setting : Code Runner : Run in Terminal.
Untick the box.
Now when you select Run Code (or CTRL + ALT + N) option when running your code, the Output window should work.
Note
The integrated terminal will not show code output now.
The output window in VScode is read-only. My method is not suitable if you are taking input from user.
I had the same problem, but figured that close to top right of your Visual Studio Code there is a little green triangle saying "running code in Terminal".
I you wanted to see some kind of print('Hello') it is does the job.
I ended up scrapping code and moving my project to Visual Studio 19. Works just fine now. Prints show up in a command prompt.

Categories