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

Support IPython for Shift+Enter (execute line in terminal) #17172

Open
brettcannon opened this issue Aug 31, 2021 Discussed in #16744 · 16 comments
Open

Support IPython for Shift+Enter (execute line in terminal) #17172

brettcannon opened this issue Aug 31, 2021 Discussed in #16744 · 16 comments
Assignees
Labels
area-terminal community ask Feature request that the community expressed interest in feature-request Request for new features or functionality needs proposal Need to make some design decisions

Comments

@brettcannon
Copy link
Member

brettcannon commented Aug 31, 2021

Discussed in #16744

Originally posted by sergeyf July 20, 2021

Environment data

  • VS Code version: 1.58.2
  • OS and version: Windows_NT x64 10.0.19043
  • Python version (& distribution if applicable, e.g. Anaconda): 3.9,1
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): conda
  • Value of the python.languageServer setting: pylance-2021.7.4

Expected behaviour

Pressing Shift + Enter copies code to the terminal and executes it

Actual behaviour

Pressing Shift + Enter copies code to the terminal but it doesn't execute

Steps to reproduce:

image

EDIT: #18177 could resolve this.

@brettcannon brettcannon added feature-request Request for new features or functionality area-terminal needs decision labels Aug 31, 2021
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Aug 31, 2021
@brettcannon brettcannon removed the triage-needed Needs assignment to the proper sub-team label Sep 1, 2021
@brettcannon
Copy link
Member Author

Thanks for the feature request! We are going to give the community 60 days from when this issue was created to provide 5 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue.

@brettcannon brettcannon added needs community feedback Awaiting community feedback and removed needs FR policy labels Sep 1, 2021
@sergeyf
Copy link

sergeyf commented Sep 1, 2021

Thank you!

@ryan-feeley
Copy link

ryan-feeley commented Sep 18, 2021

This issue is funny, because sometimes I get the desired behavior already, and sometimes I don't, depending on my conda environment.

Environment A from CONDA-FORGE:
This environment gives me the normal behavior where code is copied and properly executed:
conda create --name py37A --channel conda-forge python=3.7 ipython ipykernel

So if I highlight the following and do shift+enter:

x = 5

y = x**2

def squareIt(x):
    return x**2

The IPython console looks like this:

In [1]: x = 5

In [2]: y = x**2

In [3]: def squareIt(x):
   ...:         return x**2
   ...: 

In [4]: 

Environment B from DEFAULTS (Anaconda):
This environment gives me the bug behavior where code is copied but not executed:
conda create --name py37B --channel defaults python=3.7 ipython ipykernel

So if I highlight the following and do shift+enter:

x = 5

y = x**2

def squareIt(x):
    return x**2

The IPython console looks like this:

In [1]: x = 5
   ...: y = x**2
   ...: def squareIt(x):
   ...:     return x**2
   ...: 
   ...: 

I'm on Windows 10, and the latest install of VSCode, but no insiders channel, or similar.

I don't know if the difference in behavior is from the conda-forge channel having slightly newer packages, or from some deeper differences. Updating the prompt_toolkit package in Environment B from the conda-forge channel (which updates several dependencies) is enough to allow that environment to have the "no bug expected behavior".

@ryan-feeley
Copy link

ryan-feeley commented Sep 18, 2021

Since it might not be clear, here is how I currently get SHIFT+ENTER to send code to IPython. I first run a dummy line of code and do SHIFT+ENTER. This opens a terminal, and runs (sometimes...) the code in the normal python REPL. Then I type exit() to exit the python REPL. At the prompt I type ipython to start the IPython REPL. Now subsequent uses of SHIFT+ENTER send the code to IPython. With the code automatically run, or not, depending on my conda environment, as mentioned in my previous comment.

I think you can achieve the same behavior of running in the IPython terminal by adding this to your settings.json:

"python.terminal.launchArgs": [
     "-m",
    "IPython"
    "--no-autoindent",
],

(Just for reference, it appears that this issue has been around for awhile, and inconsistently reproduced. Perhaps the conda channel choice is the key to understanding the different observations. #169; ipython/ipython#13054). You'll also see various 3rd party VSCode extensions that people have created to try to work around this issue.

@ryan-feeley
Copy link

There have been various hacks proposed on the github issues and SO adding delays, and the like, to cause IPython to actually run the commands sent with SHIFT+ENTER. Perhaps the differences I observe based on package channel are somehow causing needed delays so occur automatically, by say using an unoptimized pyqt, event loop, or something, in the background to process the REPL on IPython side.

@brettcannon
Copy link
Member Author

Thank you to everyone who upvoted this issue! Since the community showed interest in this feature request we will leave this issue open as something to consider implementing at some point in the future.

We do encourage people to continue 👍 this issue as it helps us prioritize our work based on what the community seems to want the most.

@brettcannon brettcannon added community ask Feature request that the community expressed interest in needs proposal Need to make some design decisions and removed needs community feedback Awaiting community feedback labels Nov 1, 2021
@sergeyf
Copy link

sergeyf commented Nov 2, 2021

Mysteriously, this now works on the same machine & env where it did not work when I opened #16744. Was there something changed in a recent VSCode release that might account for it working now?

@ryan-feeley
Copy link

Mysteriously, this now works on the same machine & env where it did not work when I opened #16744. Was there something changed in a recent VSCode release that might account for it working now?

@sergeyf , check my comment here. I've had this work properly with and environment taken from conda-forge, and not work with one from Anaconda.

So make sure it isn't just your environment, but where you're getting your builds from.

@sergeyf
Copy link

sergeyf commented Nov 2, 2021

Thanks @ryan-feeley. Pretty sure I haven't changed my environments since reporting originally. Just updated VSCode.

@DEVNULLDNE
Copy link

I have this problem off and on as well. You can get around it if you have the multi-command extension by adding the following to your keybindings.json:

    {
        "key": "shift+enter",
        "command": "-python.execSelectionInTerminal",
        "when": "editorTextFocus && !findInputFocussed && !jupyter.ownsSelection && !notebookEditorFocused && !replaceInputFocussed && editorLangId == 'python'"
    },
    {   // use this when ipython gets borked using "python.execSelectionInTerminal"
        "key": "shift+enter",
        "command": "extension.multiCommand.execute",
        "args": {
            "interval": 40,
            "sequence": [
                "python.execSelectionInTerminal",
                "workbench.action.terminal.focus",
                "workbench.action.terminal.scrollToBottom",
                {"command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }}, // this basically "presses enter"
                "workbench.action.focusActiveEditorGroup"
            ]
        },
        "when": "editorTextFocus && !findInputFocussed && !replaceInputFocussed && editorLangId == 'python'"
    },

I stole the solution from here with some slight modifications. Mostly adding an interval as the commands were happening too quickly sometimes.

@flcong
Copy link

flcong commented Dec 16, 2021

I have this problem off and on as well. You can get around it if you have the multi-command extension by adding the following to your keybindings.json:

    {
        "key": "shift+enter",
        "command": "-python.execSelectionInTerminal",
        "when": "editorTextFocus && !findInputFocussed && !jupyter.ownsSelection && !notebookEditorFocused && !replaceInputFocussed && editorLangId == 'python'"
    },
    {   // use this when ipython gets borked using "python.execSelectionInTerminal"
        "key": "shift+enter",
        "command": "extension.multiCommand.execute",
        "args": {
            "interval": 40,
            "sequence": [
                "python.execSelectionInTerminal",
                "workbench.action.terminal.focus",
                "workbench.action.terminal.scrollToBottom",
                {"command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }}, // this basically "presses enter"
                "workbench.action.focusActiveEditorGroup"
            ]
        },
        "when": "editorTextFocus && !findInputFocussed && !replaceInputFocussed && editorLangId == 'python'"
    },

I stole the solution from here with some slight modifications. Mostly adding an interval as the commands were happening too quickly sometimes.

Works well!

@m-ganko
Copy link

m-ganko commented Dec 13, 2022

I have this problem off and on as well. You can get around it if you have the multi-command extension by adding the following to your keybindings.json:

    {
        "key": "shift+enter",
        "command": "-python.execSelectionInTerminal",
        "when": "editorTextFocus && !findInputFocussed && !jupyter.ownsSelection && !notebookEditorFocused && !replaceInputFocussed && editorLangId == 'python'"
    },
    {   // use this when ipython gets borked using "python.execSelectionInTerminal"
        "key": "shift+enter",
        "command": "extension.multiCommand.execute",
        "args": {
            "interval": 40,
            "sequence": [
                "python.execSelectionInTerminal",
                "workbench.action.terminal.focus",
                "workbench.action.terminal.scrollToBottom",
                {"command": "workbench.action.terminal.sendSequence",
                    "args": { "text": "\u000D" }}, // this basically "presses enter"
                "workbench.action.focusActiveEditorGroup"
            ]
        },
        "when": "editorTextFocus && !findInputFocussed && !replaceInputFocussed && editorLangId == 'python'"
    },

I stole the solution from here with some slight modifications. Mostly adding an interval as the commands were happening too quickly sometimes.

From vs code version higher than 1.71 it seems not to work properly any more. When I am running more than one line, each line is executed separately.

I found a solution to simulate 'ctrl+o' shortcut as a first step, which is adding line break in ipython terminal. The only problem is that any comment in code breaks this.
Does anyone have idea how to fix this?

Here is the code which needs to be added as first sequence step:

{"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u000F" }}

@peter-chang62
Copy link

Thanks for the solution! If you don't mind, having to configure an interval in between commands seems a bit forced to me. It works fine, but I was wondering if there are other solutions?

As a note, I've tried installing different versions of python (3.7 - 3.11), with ipython installed from conda-forge and Anaconda but all of them have the same bug. I am on Windows 10.

@Garcia-INPE
Copy link

Garcia-INPE commented Oct 17, 2024

Now, almost in 2025, this issue still happens. In my case, running a selected text in terminal (not in REPL windows, not in IPython), the indentation has weird behavior in terminal output, it seems like each line gains additional blank spaces as lines goes down and I don't why, like this:

1st Shift+Enter                2nd Shift+Enter                3rd Shift+Enter
---------------                ---------------                ---------------
>>> def test():                >>> def test():                >>> def test():
...         x = 1              ...             x = 1          ...                     x = 1
...             y = 2          ...                 y = 2      ...                         y = 2
...                 z = 3      ...                     z = 3  ...                             z = 3


IN ALL OF THEM: 

File "<python-input-2>", line 3
    y = 2
IndentationError: unexpected indent

The "good news" is that my tests suggested me that it is a matter related with the Python version and not with Anaconda. The behavior above was seen when I run the selected text with Shift+Enter using the lasted Python at the moment, i.e. 3.13.0, which was automatically installed in my Conda env (not the base) as no Python version was specified.

When I downgraded Python version to 3.12.7 (the last 3.12.x), every thing worked fine:

>>> def test():
...     x = 1
...     y = 2
...     z = 3

After getting struggle for some time trying a lot of solutions, that's my take, I hope this can help.

@anthonykim1
Copy link

anthonykim1 commented Dec 10, 2024

Thank you all for showing interest for iPython support and continuing to show interest via upvoting the issue itself.

In the meantime, I just wanted to inform everyone we have something called Native REPL now: https://code.visualstudio.com/docs/python/run#_native-repl which should provide useful features like intellisense, syntax highlighting (although it is an editor stye based REPL).

I'm very curious to see what people think about this (goods and bads compared to ipython, cpython IDLE, other REPL experiences you have had with Python), so we have better sense of what the community really wants in regards to REPL in VS Code going forward.

Best,

@cbrnr
Copy link

cbrnr commented Dec 11, 2024

I've commented in #22139 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-terminal community ask Feature request that the community expressed interest in feature-request Request for new features or functionality needs proposal Need to make some design decisions
Projects
None yet
Development

No branches or pull requests