Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

atom editor indentation error with Python

I am new to atom, so I opened my existing code using atom and modified few lines, then when I tried running the code with python, I get the following error:

IndentationError: unindent does not match any outer indentation level

I realized that Atom editor does indent my code differently to what I had. refer to the attached picture below showing the different indentation styles. line 1300 is the old indentation and 1301 is the one created by Atom

How can I fix this without modifying my 1000+ line code and so that atom uses the same style of indentation.

enter image description here

like image 444
Ossama Avatar asked Jun 09 '14 10:06

Ossama


People also ask

How do you fix an atom indentation in Python?

Highlight the lines of code you want to re-indent, and activate the re-indent command either through the command palette ( ctrl-shift-p ) or by using the keybinding ctrl-shift-r .

How do I fix indent expected error in Python?

This error can also come up if the programmer forgets to indent a docstring. Docstrings must be in line with the rest of the code in a function. To fix this issue, indent the docstring.

Can we handle indentation error in Python?

Practical Data Science using Python A IndentationError occurs any time the parser finds source code that does not follow indentation rules. We can catch it when importing a module, since the module will be compiled on first import.

How do I fix the TabError in Python?

The Python “TabError: inconsistent use of tabs and spaces in indentation” error is raised when you try to indent code using both spaces and tabs. You fix this error by sticking to either spaces or tabs in a program and replacing any tabs or spaces that do not use your preferred method of indentation.


2 Answers

In the newer versions of Atom, just go to settings > Editor; then scroll down to tab length and change it to 4; then change tab type to soft. This will make the tab key insert 4 spaces instead of a tab character moving forward. You still have to update your previous code using one of the above methods.

like image 148
Simon Serrano Avatar answered Oct 14 '22 22:10

Simon Serrano


You have mixed tabs and spaces in your code. You should use spaces, always.

You can use this plugin to quickly fix your code, and please, use only spaces and 4 spaces for each level of indentation.

like image 44
Burhan Khalid Avatar answered Oct 14 '22 22:10

Burhan Khalid