vscode-python: Run Selected Text in Active Terminal gives IndentationError with IPython

Environment data

  • VS Code version: 1.40.2
  • Extension version (available under the Extensions sidebar): 2019.11.50794
  • OS and version: macOS Catalina 10.15.1
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.5 Anaconda defaults
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): conda
  • Relevant/affected Python packages and their versions: XXX
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info #3977): Language Server

Expected behaviour

Selecting some text and running the command “Terminal: Run Selected Text In Active Terminal” properly pastes the selection to an IPython console.

Actual behaviour

IndentationError is received.

Steps to reproduce:

  1. Launch IPython in the built-in terminal. Then open some file in the editor; type and select
for x in range(3):
    print(x)
    break
  1. Execute the command “Terminal: Run Selected Text in Active Terminal”

The following error is received:

In [1]: for x in range(3): 
   ...:         print(x) 
   ...:             break                                                                              
  File "<ipython-input-1-6c59f84e067e>", line 3
    break
    ^
IndentationError: unexpected indent

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

No output

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging)

No output

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 13
  • Comments: 31 (10 by maintainers)

Most upvoted comments

Try to toggle ipython’s %autoindent into OFF.

https://ipython.org/ipython-doc/3/interactive/reference.html#autoindent

Autoindent is ON by default, but it can cause problems with the pasting of multi-line indented code (the pasted code gets re-indented on each line). A magic function %autoindent allows you to toggle it on/off at runtime. You can also disable it permanently on in your ipython_config.py file (set TerminalInteractiveShell.autoindent=False).

The above is out of date, but useful. For ipython_config.py creation, refer to the latest ipython documentation link below. https://ipython.readthedocs.io/en/stable/config/intro.html#python-configuration-files

Same issue for me on ubuntu

Ah, thanks for the clarification. We’ll look into this further.

Thanks. I’ve experimented a bit more, this time with the command from the Python extension (Python: Run Selection/Line in Python Terminal), and have concluded that the issue is with IPython support, regardless of the use of Python: ... vs Terminal: ...

Specifically if you do the following:

  1. Select some arbitrary text, execute Python: Run Selection/Line in Python Terminal – now you have a "Python Terminal" in this terminal instance.
  2. exit() this prompt, now you have a regular shell prompt in this current terminal instance.
  3. Launch ipython here

Now, the Python: Run Selection ... command does send the selection to the IPython prompt, but we again have the same indentation issue.

So my question is,

  1. For both commands (Terminal: ... and Python: ...) it appears that the behavior is the same. Does the Python extension currently rely on the same underpinnings for the “send selection” behavior as VSCode proper?
  2. If so, could the extension customize its own command so that it works better with IPython (I think the problem is due to some issue with handling newlines)

Thank you again for your time!

@ericsnowcurrently I’ve tested this with VSCode & Python extension releases from around November 2019 (which is when I’d thought this might have been working). Unfortunately still seeing the same issue with IndentationError.

I must have been misguided in thinking this actually worked at some point. (Apologies for the (great) delay on this feedback)

I’m experiencing now the same error related in this issue, but it did not happen in an recent past.

I’ve updated Anaconda in the last month to use Python 3.9. That’s the only modification from the point I believe this error did not occur.

Set %autoindent to OFF solves the problem, for now, but now I need to set it as default in IPython.

Additional infos:

  • Pasting in the terminal with ctrl+v leads to the same error
  • Pasting in the terminal with right click leads to the same error
  • Pasting in the terminal with %paste works fine
  • Pasting in a terminal outside VS Code with ctrl+v works fine (gif1 below)
  • Pasting in a terminal outside VS Code with right click leads to the same error (gif2 below)

ctrl+v gif1

right click gif2

I’m on Windows 10.0.17763.1935, Anaconda 2021.11, Python 3.9, IPython 7.29.0

Same issue as @ECon87 and @iutlu on Mac OS, %paste or just cmd+v works fine, but right click then “Run Selection/Line in Python terminal” fails.

The sending code to “interactive python” (i.e. cmd+enter) seems to work except for the misleading output on the read-only editor:

image

The code on the editor is

def myf(x):
  """ doc string
  """
  return x

We’ll look into it. I suspect there is something fishy going on with IPython. The extension literally pastes the highlighted text into the terminal. This works fine in the REPL for the same interpreter in which we run IPython. That’s why I think IPython is responsible. We’ll find out and if there’s anything we can do about it then we’ll work on it.

(Note that the extension does a little normalization to make sure line feeds and unicode are right. However, I verified that the example text from above does not get changed.)