8

I am trying out Visual Studio Code, to learn Python.

I am writing a starter piece of code to just take an input from the user, say:

S = input("What's your name? ")

When I try to run this (Mac: Cmd + Shift + B) I see the task is running with no output. I have already configured the tasks.json file for output and arguments.

print("Hello, World!")
S = input("What's your name? ")

Do I need to configure some environment variables in Visual Studio Code?

2
  • Try running your script from a cmd shell. Commented Sep 19, 2015 at 20:12
  • Thank you Keith. It worked from the command line, watched a few videos to figure out the cmd line and got it working. I would really like to run the code right in VS Code and see the output there, maybe i have to try another IDE. Commented Sep 19, 2015 at 23:44

9 Answers 9

5

Tasks are intended for building your application. Since Python is interpreted, you don't need to use tasks.json at all to run/debug your Python code. Use the launch.json instead. I am using Don Jayamanne's Python extension for debugging and have configured the launch.json as follows:

  1. Open the Command Palette (Ctrl + Shift + P) and write the command:

    start without debugging

  2. Then select your Environment -> Click Python. This should create a launch.json file within a .vscode directory in the current directory.

  3. Paste the following configuration json

    {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config.python.pythonPath}",
            "program": "${file}",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ],
            "console": "integratedTerminal"
        }
    ]}
    
  4. Save the file, open your python script in the editor and 'start without debugging' again. This should launch an integrated terminal where you can give input as well as see the output.

Sign up to request clarification or add additional context in comments.

1 Comment

Please mark as the accepted answer if you feel this helped. TIA.
4

Ctrl+Shift+d, then choose integrated terminal/console.

enter image description here

Comments

2

You could install the Python extension for Visual Studio Code from the Visual Studio Code market place.

Once done, use the "Python Console" debug option to run and debug your Python code. This will launch the terminal/command window allowing you to capture input, and you wouldn't need to configure the tasks.json file for this.

Python extension: https://marketplace.visualstudio.com/items?itemName=donjayamanne.python

Comments

1

type your code in Interactive window e.g.

age = input('enter a num') 
print(type(age))

then execute code.

Now it is waiting to you for enter a number as age. Enter a number in empty bar and up of your vscode window that is written "enter a num" under the it, and press Enter. As a result you can see bellow aswer in interactive window:

<class 'str'>

and as follow you can see testing your code

testing your code

Comments

1

This helped me: instead of clicking on the "Run Code" option, click on "Run Python file", in the right corner.

Comments

1

February 2023 answer in 3 easy steps:

  1. Install Code Runner.
  2. Open Settings ( Ctrl + ,)
  3. Search and tick the box for 'Code Runner: Run In Terminal' . DONE. enter image description here

Comments

0

When you click the debug option, it takes you to the debug console instead of an actual integrated terminal. This is because the debug console only shows your code is running smoothly, but it doesn't actually allow you to add input.

I have already tried Don's suggestion and sadly it doesn't work. What you said originally by configuring the .json file was correct. With Visual Studio Code , you can only "work with" your code on a command line. Hopefully that gets changed in the future.

Comments

0

In vscode Terminal tab type:

python3 file_name.py

Comments

0

Using Visual Studio code 1.66.0 with Pylance and launch.json config on Windows 10x64

While all my code executed in the internal debugger, the INPUT code would not. I could not use the integrated terminal for input.

I applied the configuration by Keshan Nageswaran. I did, however, have to comment out, "pythonPath": "${config.python.pythonPath}", as VSC came back with the alert, The Python path in your debug configuration is invalid.

That being said, I am extremely grateful for the config code. I was able to respond to the input in the integrated terminal. My input was visible in the integrated terminal and reflected in the internal debugger.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.