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:
- 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
- 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 (View
→Output
, 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)
Try to toggle ipython’s %autoindent into OFF.
https://ipython.org/ipython-doc/3/interactive/reference.html#autoindent
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 ofPython: ...
vsTerminal: ...
Specifically if you do the following:
Python: Run Selection/Line in Python Terminal
– now you have a"Python Terminal"
in this terminal instance.exit()
this prompt, now you have a regular shell prompt in this current terminal instance.ipython
hereNow, 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,
Terminal: ...
andPython: ...
) 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?Thank you again for your time!
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:
ctrl+v
leads to the same error%paste
works finectrl+v
works fine (gif1 below)ctrl+v
right click
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 justcmd+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:The code on the editor is
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.)