TabError: inconsistent use of tabs and spaces in indentation
I keep getting this kind of error when I write functions even though I always am consistent with the tabs.
It happens mainly when I want to edit and add something in my function.
Any tips please?
If you post up an example, it may help with any advice as to what you should be doing.
Also which IDE are you using?
From PEP8 - Style Guide for Python Code:
Tabs or Spaces?
Spaces are the preferred indentation method.
Tabs should be used solely to remain consistent with code that is already indented with tabs.
Python disallows mixing tabs and spaces for indentation.
Yes, but isnāt the convention after ifās and after functionās to add a tab?
Itās ridiculous. I use notepad++. When I look at the editor everything seems aligned perfectly but when i ctrl+v you can see the difference. Iām confused
Iād suggest that you donāt use the Tab
character at all. Itās been sometime since I used Notepad++
so Iām unsure if thereās an option to replace the Tab
with four spaces, but from memory, itās a very good text editor, so Iād be surprised if that option is not implemented.
Ok, Iāll try, thank you
Bear in mind that this is only for the Python standard library itself. Outside of that, youāre welcome to pick either tabs or spaces, just as long as youāre consistent. I personally prefer tabs, since that eliminates the debates about whether 4 spaces or 2 or whatever other number is correct. You can control the displayed width of a tab in most editors, but you canāt usually change the displayed width of leading spaces.
Notepad++ Settings => Preferences => Languages has a Tab Settings box with a line for [default]
and each language, including python. For each line, one can set 'Tab size ` and check ā Replace by spaceā (and ā Use defaultā for language lines). For Python, I have size 4 and replace checked.
General advice:
Thx but Iām still very confused. Take a look if you can at this simple code for example.
It tells me that indentation Error for Line 7 but it is perfectly alinged with the rest of the lines. No matter how much I play with, try to go back, use 4 spaces, tabs, whatever, it wonāt work. I just have to write it all again
Unfortunately, itās not possible to tell whatās going on, from an image file. If you share with me the erroneous file, Iāll gladly take a look and see if I canāt solve this for you.
To add: you could do some basic analyses of the erroneous file, yourself, with this simple script:
with open('temp.py', mode='r', encoding='UTF-8') as read:
for line in read:
for char in line:
print(char, ord(char))
Replace the 'temp.py'
with the filename that you want to analysis and see what output you get.
The output from this when it reads itself, This is the output (cropped for brevity):
w 119
i 105
t 116
h 104
32
o 111
p 112
e 101
n 110
( 40
' 39
t 116
e 101
m 109
p 112
. 46
p 112
y 121
' 39
, 44
32
m 109
o 111
d 100
e 101
= 61
' 39
r 114
' 39
, 44
32
e 101
n 110
c 99
o 111
d 100
i 105
n 110
g 103
= 61
' 39
U 85
T 84
F 70
- 45
8 56
' 39
) 41
32
a 97
s 115
32
r 114
e 101
a 97
d 100
: 58
10
32
32
32
32
f 102
o 111
r 114
32
l 108
i 105
n 110
e 101
32
i 105
n 110
32
r 114
e 101
a 97
d 100
: 58
10
32
32
32
32
32
32
32
32
f 102
o 111
r 114
32
...
So, the 32
s are spaces and the 10
s are a linefeed and are the only ānone printableā characters you should see. If you see other ānone printableā, then thatās the character to look into.
Try configuring Notepad++ to show the spaces and tabs: View->Show Symbol->SHow White Space and TAB.
That might help you see what might be wrong.
It wonāt allow to post the acutal file unfortunately
You should be able to copy/paste the code. In your editor, āselect allā, then "copyā. In a reply to this thread, type three backticks, āpasteā, then three more backticks:
<paste your code here>
I use TextPad and I have it set to automatically convert tabs to spaces. Problem solved.