Search code examples
pythonpowershellvisual-studio-codeterminalcommand-prompt

Python wont run inside CMD, PS, or VS Code


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:\Users\sajja\Documents\git\cs50w_repo\cs50w_lecturepractice\lecture2-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:\Users\sajja\Documents\git\cs50w_repo\cs50w_lecturepractice\lecture2-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:\Users\sajja\Documents\git\cs50w_repo\cs50w_lecturepractice\lecture2-python\name.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


Solution

  • 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):

      • screenshot with integrated terminal
    • 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 the OUTPUT 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.

        • VSCode offers to do that for you the first time you open a Python script for editing.
      • 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 the input() 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 Run and Debug view (selectable via the Run and Debug icon icon in the leftmost side bar), the command palette (Ctrl-Shift-P) or via the playback icon to the right of the editor pane:

          • screenshot with playback icon