Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code - input function in Python

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?

like image 493
bhanu Avatar asked Sep 19 '15 18:09

bhanu


People also ask

How do I input Python code in Visual Studio?

From the Command Palette (Ctrl+Shift+P), select the Python: Start REPL command to open a REPL terminal for the currently selected Python interpreter. In the REPL, you can then enter and run lines of code one at a time.


2 Answers

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.

like image 88
mrsauravsahu Avatar answered Sep 23 '22 06:09

mrsauravsahu


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

enter image description here

like image 37
Xin Chou Avatar answered Sep 26 '22 06:09

Xin Chou