question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"IndentationError: unexpected indent" when evaluating indented blocks of code

See original GitHub issue

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)

Issue Analytics

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

github_iconTop GitHub Comments

10reactions
jjnurminencommented, Apr 4, 2019

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

8reactions
chris-larsencommented, Feb 10, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

What should I do with "Unexpected indent" in Python?
If you got an unexpected indent and you see that all the code is perfectly indented, try opening it with another editor and...
Read more >
How do i resolve the unexpected indent error in python
As the error message indicates, you have an unexpected indent error. This error occurs when a statement is unnecessarily indented or its ...
Read more >
How to fix indentation Error in Python - Numpy Ninja
Unexpected indent - This line of code has more spaces at the beginning than the one before it, but the one before it...
Read more >
What is unexpected indent in Python? - Net-Informations.Com
As the error message indicates, you have an indentation error . This error occurs when a statement is unnecessarily indented or its indentation...
Read more >
src python in org-mode: IndentationError: unexpected indent
It is just because there is unexpected indent in the source block. It is best if you edit the source block with org-edit-src-code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found