Help in Error I keep getting

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


Error on line 7…

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 32s are spaces and the 10s 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.

1 Like