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

Run Selected Text in Active Terminal gives IndentationError with IPython #8945

Closed
iutlu opened this issue Dec 6, 2019 · 32 comments
Closed
Assignees
Labels
area-terminal bug Issue identified by VS Code Team member as probable bug needs PR Ready to be worked on

Comments

@iutlu
Copy link

iutlu commented Dec 6, 2019

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 How to update the language server to the latest stable version #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

@iutlu iutlu added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels Dec 6, 2019
@iutlu
Copy link
Author

iutlu commented Dec 6, 2019

A few more notes: The regular Python prompt is OK. Also, I've used IPython + Run Selected Text with my Windows machine for quite a few months with no issues. I've recently moved to a Mac and I've run into this bug just now. I cannot test with Windows at the moment so I'm not sure if this is a Windows vs. Mac issue, or a general regression.

@iutlu
Copy link
Author

iutlu commented Dec 6, 2019

I've checked this on Windows (the same VSCode & Python extension versions), the problem is not there. So possibly it's macOS specific?

@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Dec 9, 2019
@ericsnowcurrently
Copy link
Member

Thanks for getting in touch with us about this, @iutlu. The Terminal: Run Selected Text In Active Terminal command is from VS Code and doesn't know about Python code. For Python you want to use the Python: Run Selection/Line in Python Terminal command.

@ericsnowcurrently ericsnowcurrently added the info-needed Issue requires more information from poster label Dec 9, 2019
@iutlu
Copy link
Author

iutlu commented Dec 10, 2019

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
Copy link
Member

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

@ericsnowcurrently ericsnowcurrently added data science and removed info-needed Issue requires more information from poster labels Dec 10, 2019
@ericsnowcurrently ericsnowcurrently removed their assignment Dec 10, 2019
@ericsnowcurrently ericsnowcurrently added triage-needed Needs assignment to the proper sub-team and removed triage labels Dec 10, 2019
@greazer
Copy link
Member

greazer commented Dec 12, 2019

@ericsnowcurrently , based on @iutlu last response, this doesn't sound like it has to do with ipython, but rather with what is being sent to the terminal from the editor. This doesn't look like a datascience or ipython issue.

@ericsnowcurrently ericsnowcurrently self-assigned this Dec 12, 2019
@ericsnowcurrently
Copy link
Member

I'll take a look then.

@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Dec 12, 2019
@ericsnowcurrently
Copy link
Member

@iutlu, Yeah, the extension calls essentially the same API to "run selected text ..." that VS Code uses itself. The key differences are:

  • the extension always uses a terminal called "Python"
  • if one doesn't exist, the extension creates it (and starts the REPL for the currently selected interpreter in it)
  • it assumes that the terminal is running a Python REPL

Note that the extension's Python: Run Selection/Line in Python Terminal command does not have any awareness of ipython. You may want to try the Python: Run Selection/Line in Python Interactive Window command.

@ericsnowcurrently
Copy link
Member

All that said, if your intention is still to run selected text in the terminal (where you are running ipython) then it sounds like the problem is that you're running into unexpected indents when the text is pasted to the terminal. Is that right?

If this is the same code that you were using on Windows is it possible that there are hidden carriage-return characters still there that are causing a problem?

@ericsnowcurrently ericsnowcurrently added the info-needed Issue requires more information from poster label Dec 17, 2019
@iutlu
Copy link
Author

iutlu commented Dec 19, 2019

@ericsnowcurrently Thank you for looking into this. I'm aware of the Interactive Window but I really prefer a simple IPython prompt (faster; can be placed at the bottom; doesn't take much space; I don't use inline-plots or cells anyways).

All that said, if your intention is still to run selected text in the terminal (where you are running ipython) then it sounds like the problem is that you're running into unexpected indents when the text is pasted to the terminal. Is that right?

Yes, I receive unexpected indents when I use any of the two commands. A manual copy-and-paste is fine.

If this is the same code that you were using on Windows is it possible that there are hidden carriage-return characters still there that are causing a problem?

I have tried the LF and CRLF versions of the same 3-line py file on both Windows and Mac, with no differences: a manual copy-and-paste always works on both platforms. The Run Select{ed, ion} ... commands always work on Windows, and always fail on Mac with an unexpected indent.

Although I'm not 100% sure, I think things did actually work as expected when I first set up VSCode on the Mac. I'll try to test with the recent few releases of VSCode and/or the extension see if/when things broke.

@ericsnowcurrently ericsnowcurrently removed the info-needed Issue requires more information from poster label Dec 19, 2019
@ericsnowcurrently
Copy link
Member

@iutlu, any updates on what you found when you tried earlier versions?

@ericsnowcurrently ericsnowcurrently added the info-needed Issue requires more information from poster label Jan 8, 2020
@iutlu
Copy link
Author

iutlu commented Jan 16, 2020

Hi, I've been a bit busy these past few days -- I'll try to look at it over the weekend

@ericsnowcurrently
Copy link
Member

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.)

@ericsnowcurrently ericsnowcurrently added area-terminal needs PR and removed info-needed Issue requires more information from poster triage labels Feb 4, 2020
@ericsnowcurrently ericsnowcurrently removed their assignment Feb 4, 2020
@ghost
Copy link

ghost commented Feb 13, 2020

I also noticed the indentation errors when using ipython in WSL terminal, but not when in cmd, powershell, and git bash

@ECon87
Copy link

ECon87 commented May 5, 2020

I face the same issue with the terminal and python (not ipython) when I want to process multi-line. The behavior is erratic. Sometimes the list passes through just fine, other times it does not.

I have a multi-line(300 lines) list of tuples of strings. At some point one of the strings (randomly) is not read correctly and a syntax error is thrown followed by series of Indentation Errors.

Details of my version:

Version: 1.44.2
Commit: ff915844119ce9485abfe8aa9076ec76b5300ddd
Date: 2020-04-16T17:07:18.473Z
Electron: 7.1.11
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Darwin x64 17.7.0
(High Sierra)

P.S.: Everything works fine in the interactive window, but i prefer the terminal since it's way faster

@brauliobarahona
Copy link

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

@NixBiks
Copy link

NixBiks commented Oct 23, 2020

Same issue for me on ubuntu

@iutlu
Copy link
Author

iutlu commented Feb 20, 2021

@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)

@brauliobarahona
Copy link

When back to VS code, but found same issue

Screenshot 2021-06-11 at 10 23 23

Screenshot 2021-06-11 at 10 35 17

@Nathan-Furnal
Copy link

Same issue was well

@nicks515
Copy link

nicks515 commented Sep 30, 2021

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

@karrtikr
Copy link

#17690 (comment)

@juergendrachta
Copy link

juergendrachta commented Oct 18, 2021

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

Toggling %autoindent really helped for the small code snippets I needed to send to Ipython. I will investigate this further, whether problems arise when sending larges chunks of code to Ipython prompt.

@Diogo-Rossi
Copy link

Diogo-Rossi commented Jan 15, 2022

@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

@arita37
Copy link

arita37 commented Jul 8, 2023

Facing same issues in vscode 1.72 Macos

@alexarnimueller
Copy link

Facing same issues in vscode 1.80.0 on Windows 10 Enterprise

@anthonykim1
Copy link

Hello folks, we have launched native repl which provides intellisense and syntax highlighting in editor-style based REPL
Please give this a try https://code.visualstudio.com/docs/python/run#_native-repl since iPython is not officially supported by Python extension.

Will be closing this against: #17172 but please feel free to go and upvote the issue to show interest.

@anthonykim1 anthonykim1 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-terminal bug Issue identified by VS Code Team member as probable bug needs PR Ready to be worked on
Projects
None yet
Development

No branches or pull requests