Visual Studio Code - input function in Python

2024/10/11 8:27:51

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?

Answer

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.

https://en.xdnf.cn/q/69794.html

Related Q&A

DRF: how to change the value of the model fields before saving to the database

If I need to change some field values before saving to the database as I think models method clear() is suitable. But I cant call him despite all my efforts.For example fields email I need set to lowe…

keep matplotlib / pyplot windows open after code termination

Id like python to make a plot, display it without blocking the control flow, and leave the plot open after the code exits. Is this possible?This, and related subjects exist (see below) in numerous ot…

socket python : recvfrom

I would like to know if socket.recvfrom in python is a blocking function ? I couldnt find my answer in the documentation If it isnt, what will be return if nothing is receive ? An empty string ? In…

pandas read_excel(sheet name = None) returns a dictionary of strings, not dataframes?

The pandas read_excel documentation says that specifying sheet_name = None should return "All sheets as a dictionary of DataFrames". However when I try to use it like so I get a dictionary of…

Plotly: How to assign specific colors for categories? [duplicate]

This question already has an answer here:How to define colors in a figure using Plotly Graph Objects and Plotly Express(1 answer)Closed 2 years ago.I have a pandas dataframe of electricity generation m…

Using nested asyncio.gather() inside another asyncio.gather()

I have a class with various methods. I have a method in that class something like :class MyClass:async def master_method(self):tasks = [self.sub_method() for _ in range(10)]results = await asyncio.gath…

AttributeError: type object Word2Vec has no attribute load_word2vec_format

I am trying to implement word2vec model and getting Attribute error AttributeError: type object Word2Vec has no attribute load_word2vec_formatBelow is the code :wv = Word2Vec.load_word2vec_format("…

Python - Core Speed [duplicate]

This question already has answers here:Getting processor information in Python(12 answers)Closed 8 years ago.Im trying to find out where this value is stored in both windows and osx, in order to do som…

App Engine, transactions, and idempotency

Please help me find my misunderstanding.I am writing an RPG on App Engine. Certain actions the player takes consume a certain stat. If the stat reaches zero the player can take no more actions. I start…

Speed differences between intersection() and object for object in set if object in other_set

Which one of these is faster? Is one "better"? Basically Ill have two sets and I want to eventually get one match from between the two lists. So really I suppose the for loop is more like:f…