question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

Discussed in https://github.com/microsoft/vscode-python/discussions/16744

<div type='discussions-op-text'>

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

</div>

EDIT: https://github.com/microsoft/vscode-python/issues/18177 could resolve this.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:25
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
DEVNULLDNEcommented, Nov 22, 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.

2reactions
ryan-feeleycommented, 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”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can i force SHIFT+ENTER to run selection and execute it ...
I have added the setting below in vscode to launch ipython when i used shift+enter to run selection. "python.terminal.launchArgs": [ "-c" ...
Read more >
VS Code: Shift+Enter does not send code to Python Interactive
Shift + Enter is registered as a keyboard shortcut for both sending code to Terminal and for sending it to Python Interactive. I...
Read more >
The IPython Notebook — IPython 3.2.1 documentation
The notebook consists of a sequence of cells. A cell is a multi-line text input field, and its contents can be executed by...
Read more >
VSCode Python Data Science Setup, Part 9: Shift+Enter to ...
... and has data science support for #Python Scripting and Jupyter Notebooks. This is a multi-part YouTube Series that comes from my Python ......
Read more >
3.5. Python types assignment - Jean Mark Gawron
If Python does not seem to be responding to you when you execute commands that ... Then, in the same terminal window you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found