Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make VSCode aware of whether a Python REPL is running or not #23317

Open
Noricc opened this issue May 2, 2024 · 1 comment
Open

Make VSCode aware of whether a Python REPL is running or not #23317

Noricc opened this issue May 2, 2024 · 1 comment
Assignees
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

Comments

@Noricc
Copy link

Noricc commented May 2, 2024

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:

x = input()
y = input()
print(f"{x=}", y)

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, and x has the value "y = input()".

Here is what we get on the terminal:

/usr/bin/python3.9
Python 3.9.5 (default, Nov 23 2021, 15:27:38) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> x = input()
y = input()
>>> print(f"{x=}", y)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'y' is not defined

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 the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

2024-05-02 16:29:16.223 [info] > /usr/bin/python3.9 ~/.vscode/extensions/ms-python.python-2024.4.1/python_files/normalizeSelection.py
2024-05-02 16:29:16.386 [info] Send text to terminal: /usr/bin/python3.9
2024-05-02 16:29:16.674 [info] Send text to terminal: x = input()
y = input()
print(f"{x=}", y)

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:

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label May 2, 2024
@anthonykim1 anthonykim1 added feature-request Request for new features or functionality area-repl labels May 2, 2024
@anthonykim1
Copy link

Hey @Noricc Thanks for the feature request. It seems like you would appreciate some kind of progress icon that shows whether your Python command ran successfully. This is already present in Interactive Window provided via Jupyter extension.

We are working on providing Python REPL in different UI (Interactive Window for the upcoming spike but likely to be different in future). This way you do not need to install Jupyter extension/iPyKernel and stick with just using Python extension. I'm curious to get your thoughts on: #23029 for the purposes of thinking about VS Code "Native" REPL experience.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

2 participants