menu

Questions & Answers

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.

Enter image description here

If I select Python it enters debugging mode. So, there isn't any output.

Comments:
2023-01-11 09:15:40
and what happens if you switch to the "OUTPUT" tab at the bottom ?
2023-01-11 09:15:40
Save this file, then go to the Terminal tab at the bottom and type python hello.py and press Enter. This should run the code and show an Output.
2023-01-11 09:15:40
@Guillaume Output Tab shows nothing. As you can see in the screenshot when I press run then a popup comes that too is shown in screenshot.
2023-01-11 09:15:40
@user8212173 it worked. Thank you.
Answers(6) :

Using the Python Extension Run/Debug commands

Notes

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

  2. 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 an icon and dropdown in the upper right of your editor window, when editing a python script file.

enter image description here

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.
    • enter image description here
  • 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:

Enter image description here

Updated Image (1/2022) - Changed from just 'Debug' to 'Run and Debug': enter image description here

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

Enter image description here

Now your debug configuration box on the top left of the debug pane should say "Python: Current File":

Enter image description here

Visual Studio Code will add a file to your current workspace or working folder called .vscode/launch.json, and opened it for edit.

Simplest Answer: Run the file directly in a terminal

  1. Open a terminal in VS Code (View/Terminal or Ctrl-`, which is Control and the grave or "tick-mark" key, normally at the top left of keyboard above tab)
  2. 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.
  3. The output will show up in that terminal.

Notes

  • You must have a python executable on your path, or otherwise configured.
  • Your python command may be python2 or python3 on some installs (usually linux), and can be just py for the Python Launcher on Windows (auto-installs with Python).
  • This option will not use any 'launch.json' configurations, and cannot be easily used to debug with (at least not with all the built-in VS Code debug goodness).
  • You can configure the terminal in different ways, such as to launch Git Bash by default, if Git is installed, or switch between CMD and Powershell (on Windows).

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 FilePreferenceSettings.json

    "code-runner.executorMap":
{
  "python": "\"C:\\Program Files\\Python39\\python.exe\""

I have attached the screenshot for your better understanding!

enter image description here

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.

Comments:
2023-01-11 09:15:40
The fact that such a simple and essential task as viewing output from a script involves this much ink being spilled is an indication that VScode is an overhyped product.

I ended up scrapping code and moving my project to Visual Studio 19. Works just fine now. Prints show up in a command prompt.

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. Code Runner setting in VScode IDE
  • Untick the box.

Now when you select Run Code (or CTRL + ALT + N) option when running your code, the Output window should work.

Run code option on vscode

Working VScode output window

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.