"IndentationError: unexpected indent" when evaluating indented blocks of code
See original GitHub issueEnvironment data
- VS Code version: 1.27.2
- Extension version: 2018.9.0
- OS and version: macOS Sierra 10.12.6
- Python version: 3.7.0, installed via pyenv
- Type of virtual environment used: none
- Relevant/affected Python packages and their versions: none
Actual behavior
Sending a selected Python code for evaluation in the Python terminal via Run Selection/Line in Python Terminal results in "IndentationError: unexpected indent"
errors if the block of code is indented.
Example: evaluating the following selection results in an error because of the indentation of the whole block at 4 spaces:
Expected behavior
It would be useful if the evaluation of indented code worked regardless of it’s position or level of indentation in the overall code (i.e. should be first deindented to a “base level” before sending to the terminal?).
As an example, the indented code above should be sent to the terminal as if it was the following (i.e. not indented):
I tend to work “bottom-up”, evaluating/debugging smaller pieces of code that form more complex functions, and those individual pieces are obviously indented. This behavior makes this sort of workflow a little difficult.
Steps to reproduce
Use the following example code to test the behavior described above by evaluating the two commented blocks using Run Selection/Line in Python Terminal functionality.
def fn():
# block indented by 4 spaces -> results in an error
x, y = 10, 2
for i in range(x):
print(i ** y)
# block not indented -> correctly evaluated
x, y = 10, 2
for i in range(x):
print(i ** y)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:17
- Comments:21 (3 by maintainers)
Top GitHub Comments
The Spyder IDE handles ‘run selection’ fine, apparently by just unindenting the block so that the lowest indentation level is 0. IMO, this should the behavior in VSCode too. (Also, a proper IPython shell with workable history and command completion would be great! The current REPL and Jupyter notebook are somewhat lacking.)
Python is 100% unusable for me because of this, I have code that executes flawlessly in terminal, pycharm, spyder, jupyter, thonny, etc but nothing but indentation exceptions are being thrown in VSCode. The best part? I wrote it all in VSCode. It was executing fine and it started to get worse and worse, and now the code just doesn’t even wanna go.
Disabled PyLint and it worked once, but now the behavior is back to indentation errors