O'Reilly Forums: Chapter 2 Sdist Taberror: - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 2 Sdist Taberror: TabError: inconsistent use of tabs and spaces in indentation

#1 User is offline   snigti 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 11-December 11

Posted 11 December 2011 - 01:24 PM

Hi,
I have managed to build module distribution using Windows 7(setup.py sdist and install goes well), but when I import nester module Python Shell gives me an error:

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import nester
File "C:\Python32\nester.py", line 6
for each in the_list:
^
TabError: inconsistent use of tabs and spaces in indentation

Thanks in advance from Vilnius!
0

#2 User is offline   ryancspgs 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 26-November 12

Posted 26 November 2012 - 08:16 PM

No answer to this question? I have the same problem and cannot resolve. Thanks

I've tried moving the indents, spaces, tabs, etc... NOTHING works.

>>> def print_lol(the_list, level):
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item, level+1)
else:
for tab_stop in range(level):
print("\t", end='')
print(each_item)

SyntaxError: inconsistent use of tabs and spaces in indentation
0

#3 User is offline   ryancspgs 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 26-November 12

Posted 26 November 2012 - 08:20 PM

It would appear that the code doesn't maintain the indents when adding them to a thread on this forum.

View Postryancspgs, on 26 November 2012 - 08:16 PM, said:

No answer to this question? I have the same problem and cannot resolve. Thanks

I've tried moving the indents, spaces, tabs, etc... NOTHING works.

>>> def print_lol(the_list, level):
for each_item in the_list:
if isinstance(each_item, list):
print_lol(each_item, level+1)
else:
for tab_stop in range(level):
print("\t", end='')
print(each_item)

SyntaxError: inconsistent use of tabs and spaces in indentation

0

#4 User is offline   paulbarry 

  • Advanced Member
  • PipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 251
  • Joined: 20-August 09

Posted 27 November 2012 - 01:41 AM

This problem has to do with the fact that the general rule in Python is that the interpreter uses indentation to indicate blocks of code. This is only half-right: Python actually uses white-space to determine the level of indentation it is currently working at.

The TAB character counts as one white-space character as does the SPACE. When you mix TABs and the use of four SPACEs in your code, it looks OK to your eyes as the indentation distances match.... however, to Python, one TAB is one white-space character, whereas four SPACEs are four white-space characters. To Python, the code with the TAB is one level of indentation deep, whereas the code with the four SPACEs is *four* (!!!) levels of indentation deep.

And that's the problem. You have an inconsistent use of TABs and SPACEs in your code.

To fix this in IDLE, bring your code into the IDLE edit window, then select EDIT... SELECT ALL... to highlight all of the code. Then select FORMAT... UNTABIFY REGION... to convert all of your TABs into four SPACEs. That should fix things.

Also... it's probably a good idea to configure your editor to automatically replace a single key-press of the TAB key with four SPACEs. That should then stop this problem from happening again.

Hope this helps.

--Paul.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users