-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"IndentationError: unexpected indent" when evaluating indented blocks of code #2837
Comments
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. |
Before looking into this, we'd first need to fix #169 |
SOLVED: Been struggling with this for an hour. Something this simple wouldn't run: 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): @DonJayamanne please fix this, thanks =) |
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 |
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 |
This worked for me: In Visual Studio 2017, go to Tools > Options > Text Editor > Python > Tabs. Erase the current white spaces and indent as usual with Tab key. The red lines should disappear. |
We would need to figure out how to handle cases where you want to intentionally paste indented blocks of code, or how many people would be unhappy if this was the default behavior. |
I think the issue is primarily when you're running a selection, not pasting code. The current behavior is rough, basically have to copy and paste code elsewhere to run it. One of the many reasons I hate how Python enforces code structure. |
I am having the same problem but only for the VS Code in my mac. I got some code that I wrote in a windows vs code (no problems there), and when opening it on my mac vs code, I cannot run any block (using shift + enter). |
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.) |
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. |
@qubitron Is this possible? I just want to run indented lines of code like I can in pycharm. For example, when testing functionality within a definition. I don't want to run the whole definition, just 1-2 lines of code. I don't want to unindent, run, and reindent. |
Same issue as @gcarmiol Moreover... shift+tab and tab back is also giving me problems with flake8 indentation. Can someone please post a proper workaround or pick up this issue? Thank you so much in advance! |
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 |
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. |
Always de-dent to the first non-empty line in the selection |
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. |
Prescribed solution:
|
Thanks for addressing this, @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. |
validated |
Environment data
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.
The text was updated successfully, but these errors were encountered: