I am a learner, so please forgive me if this is common knowledge
I fixed my PATH files for python, I made the only PATH and sole instance of Python C:Python
This fixed the issue where VS code could not find a python interpreter.
Now that I have done this, when when i try to run a python command, this happens:
PS C:UserssajjaDocumentsgitcs50w_repocs50w_lecturepracticelecture2-python> python name.py
but it never prompts me for the input, which is the whole point of this file.
when i check the python install, vscode does recognize it, just wont run it
PS C:UserssajjaDocumentsgitcs50w_repocs50w_lecturepracticelecture2-python> python
Python 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
If i switch to the output tab and do it from there instead, it runs the code without error, but never prompts me for the input, and therefore never prints the output
[Running] python -u "c:UserssajjaDocumentsgitcs50w_repocs50w_lecturepracticelecture2-pythonname.py"[Done] exited with code=0 in 0.057 seconds
I was expecting for this code:
name = input("Name: ")
print(name)
to prompt me to enter an input, and then print said input in the output or terminal
2
Answers
There are many similar questions, but I could'nt find an exact one. Here is the solution I found:
Instead of running the file in VSCode, I selected Run and Debug, which opened a different terminal (maybe?), this terminal accepts input, and therefore printed the correct output.
Here it is:
The output window still does not do what it is supposed to, but the Terminal window is behaving the way it should
Note: I’m assuming that your problems are limited to running in Visual Studio Code. Assuming the
python
executable is discoverable via$env:PATH
, you should not experience problems when running your script in regular console windows or Windows Terminal.In order to run interactive console (terminal-based) code in Visual Studio Code, it must run in the integrated terminal, which is the
TERMINAL
tab in the panel (you can toggle the latter’s visibility with Ctrl-J):By contrast, the
OUTPUT
tab is not designed for or capable of running interactive console applications.It is used by extensions such as Code Runner, which, via its
Run Code
command runs code invisibly and captures its output only, which is printed to theOUTPUT
tab – any interactive prompts will actually cause the hidden process to effectively hang, because no interactive input can be provided. (There is an opt-in configuration setting that allows you to run in the terminal instead, but it only works for entire, already-saved files).It is best to install a language-specific debugger, which not only (always and by default) properly executes code in the terminal, but also supports debugging.
Specifically, for Python files:
Install the Python extension.
The run your code as follows:
F5 to run the current script in the debugger (supporting breakpoints, stepping through the code, …)
Ctrl-F5 to run your script normally.
There’s also Shift-Enter for executing the currently selected lines in an interactive
python
session, but note that this doesn’t work for interactive code snippets, because the lines are pasted one by one, so that the code line after theinput()
call is accidentally pasted as the response to the input prompt.Of course, you can also invoke run and debug functionality via the GUI, such as via the
icon in the leftmost side bar), the command palette (Ctrl-Shift-P) or via the playback icon to the right of the editor pane:
Run and Debug
view (selectable via the