vscode-python: "IndentationError: unexpected indent" when evaluating indented blocks of code

Environment 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:

image image

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

image image

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)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 17
  • Comments: 21 (3 by maintainers)

Most upvoted 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

Hello, I code in Spyder and PyCharm and both accept by default sending indented code to the console/interactive window and long as it’s a selection of code or single line. I believe also that this needs to be the default when sending sections of code to be executed. What needs to be done is that any time a block of code is sent to the interactive window, the lowest indent becomes the “no indent” and it’s all aligned according to this lowest indent.
I also want to move to VSCode but this is making it hard. Right now I have to select a block…press Shift+Tab until I get it to the edge of the window, execute it…then tab it back to it’s original indent.

Same issue as @gcarmiol I also want to move to VSCode but this is making it hard. Right now I have to select a block…press Shift+Tab until I get it to the edge of the window, execute it…then tab it back to it’s original indent.

Moreover… shift+tab and tab back is also giving me problems with flake8 indentation. After i tab back i get: ‘continuation line over-indented for visual indent’ from flake8.

Can someone please post a proper workaround or pick up this issue?

Thank you so much in advance!

This worked for me:

In Visual Studio 2017, go to Tools > Options > Text Editor > Python > Tabs. Select “Keep tabs” and click OK.

Erase the current white spaces and indent as usual with Tab key.

The red lines should disappear.

I just briefly looked for a potential solution, at it seems that the issue could be solved by adding something like this as one of the pre-processing steps in this function?

# Step X:  Remove excess indentation
indentation = re.search(r'^\s+', lines[0]).group()
if indentation:
    lines = [re.sub(indentation, '', l, count=1) for l in lines]

I have no experience with the codebase of this extension, but I’m happy to poke around more if you think this is something worth fixing.