Skip to content

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

Closed

Description

@bodkan

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)

Activity

  1. bodkan commented on Oct 10, 2018

    @bodkan
    Author

    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.

  2. DonJayamanne commented on Oct 18, 2018

    @DonJayamanne

    Before looking into this, we'd first need to fix #169

  3. syagev commented on Nov 14, 2018

    @syagev

    A quick n dirty workaround for now is to "multi-select" the code block using ALT+SHIFT:
    image

    That still won't let you right-click > Debug:Evaluate but you can copy paste it in the debug window to get the code without any leading indentations.

  4. tjaeel commented on Jan 8, 2019

    @tjaeel

    SOLVED:

    Been struggling with this for an hour. Something this simple wouldn't run:

    image

    SOLUTION: Just make sure that the LAST line you run when using (Shift + Enter) is not indented. If the last line is indented, you will either get an indentation error or be shown your code again in output

    SOLUTION EXAMPLE (this runs perfectly):

    image

    Don Jayamanne (@DonJayamanne) please fix this, thanks =)

  5. martinpeck commented on Jan 20, 2019

    @martinpeck

    User has seen this issue and posted a question related to it on Stack Overflow. I assume this issue is the cause of this user's problems.

    https://stackoverflow.com/questions/54276596/indentation-error-on-visual-studio-code-on-a-mac-again

  6. nihirisuto commented on Feb 10, 2019

    @nihirisuto

    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

  7. scorpiocavalier commented on Feb 19, 2019

    @scorpiocavalier

    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.

  8. 11 remaining items

  9. rubenl92 commented on May 8, 2019

    @rubenl92

    Same issue as Giovanni Carmiol (@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!

  10. nsymms commented on May 8, 2019

    @nsymms

    I completely agree with all of this. I probably select-and-execute code blocks a hundred times a day with pycharm or spyder. And it's impossible with VSCode.

    And I'm beginning to think I'm doing something wrong since there's so few complaints about this issue. What is everybody else doing? What debug workflow are others doing that doesn't hit this issue? Isn't everybody's code indented? Every Python script I write has almost every line indented except for the import lines at the top and the one if __name__ == '__main__' line.

  11. jjnurminen commented on May 9, 2019

    @jjnurminen

    What is everybody else doing? What debug workflow are others doing that doesn't hit this issue?

    Funny that you mentioned debugging, since for me the integrated debugger is basically the only reason to use VSCode. It saved me on many occasions where I needed a proper debugger.

    Otherwise, I have to say that VSCode-Python is pretty clunky: it's slow, there's no proper interactive shell, running code selections is pretty much impossible, the language server is still unusable and many operations just keep failing randomly (e.g. 'Run Python file in Terminal') works about 50% of the time for me).

    However it does look promising. I keep hoping that it'll be more usable in the near future.

  12. luabud commented on May 9, 2019

    @luabud
    Member

    Always de-dent to the first non-empty line in the selection

  13. mikful commented on May 23, 2019

    @mikful

    I totally agree with the points raised. This issue makes working with Python files in VS Code a real bore. Otherwise, it runs much faster for me than Spyder and I would love to keep using it, but this is becoming a real issue when evaluating sections of larger code chunks.

  14. DonJayamanne commented on May 29, 2019

    @DonJayamanne

    Prescribed solution:

    • Detent to outer most indentation
    • Modify pythonFiles/normalizeForInterpreter.py (use textwrap.dedent)
  15. nsymms commented on May 30, 2019

    @nsymms

    Thanks for addressing this, Don Jayamanne (@DonJayamanne).

    I think proposed solution is probably the best that can be done given the current architecture. The only thing missing (IMHO) would be to consider the indentation of the line where the selection starts. Knowing the original indent of the first line (when the entire line is not included in the selection -- e.g. spaces missed, some code parts intentionally omitted) would ensure the remainder of the selection is properly re-indented.

    However, looking at the code I can see that only the selected text is passed; examining the original first line from the editor is not feasible without a larger refactor.

  16. ericsnowcurrently commented on Jun 5, 2019

    @ericsnowcurrently

    validated

  17. ghost removed on Jun 5, 2019
  18. locked as resolved and limited conversation to collaborators on Jun 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions