Make VSCode aware of whether a Python REPL is running or not #23317
Labels
area-repl
feature-request
Request for new features or functionality
info-needed
Issue requires more information from poster
triage-needed
Needs assignment to the proper sub-team
Type: Feature Request
Behaviour
As far as I know, there's two ways to run Python code: run the whole file in script mode, or run the interpreter (REPL) and paste lines there (which we can do with Shift+Enter).
VSCode doesn't seem to know if a Python interpreter is running in the terminal, so it's easy to have errors if you're not careful.
For example, this can cause problems when running several lines that contains calls to the "input()" function, and then running the code with Shift+Enter.
Steps to reproduce:
Write the following piece of code:
Then highlight the lines of code, and press Shift + Enter. The REPL starts, and the selected for is sent to the REPL. The first line runs. But while Python is waiting for the user to type something, VSCode will paste the next line. Therefore, the second line doesn't run (it's treated as input). Then the third line is executed, and Python reports a NameError, because the variable
y
doesn't exist, andx
has the value "y = input()".Here is what we get on the terminal:
Similarly, if I run a piece of code with Shift+Enter, and THEN click on the button to run the whole file, VSCode crafts the command to do so, and send it to the terminal window. If an interpreter is running there, the command is not valid.
I suppose the solution would be to have a way to check in which state the terminal is in: If there is no python interpreter running, then we need to start / run it, otherwise, we need to paste python code. Handling
input()
seems a bit trickier, because you would need to detect code that uses that function, and break the code that is sent to the REPL, so that the user has time to type something in the terminal, before VSCode does.Diagnostic data
Output for
Python
in theOutput
panel (View
→Output
, change the drop-down the upper-right of theOutput
panel toPython
)Extension version: 2024.4.1
VS Code version: Code 1.88.1 (e170252f762678dec6ca2cc69aba1570769a5d39, 2024-04-10T17:34:12.840Z)
OS version: Linux x64 5.15.0-105-generic snap
Modes:
The text was updated successfully, but these errors were encountered: