I wrote a few python programs on Idle in Windows. I later shifted to gedit on Ubuntu 14.04. Both have python3 working.
I edited these files in gedit.
When I run these files, I get
'TabError: inconsistent use of tabs and spaces in indentation'
error, even when VISUALLY, there is no inconsistent tabbing or indentation done.
[Seemingly, on Windows' Idle the tabs are 4-spaces wide, whereas in gedit, the tabs currently are 8-spaces wide. So, I couldn't use TAB
directly.]
How to resolve this error and the issue?
Any solutions?
The standard answer is to configure your editors (all of them) so that you only have spaces and always four of them. For code that already exist, you have to transform all tabs into spaces (sed
can do it quickly, as should any editor/IDE you use).
Most editors should be able to insert spaces when pressing the TAB
key.
See pep8 for reference:
Spaces are the preferred indentation method.
Tabs should be used solely to remain consistent with code that is already indented with tabs.
Python 3 disallows mixing the use of tabs and spaces for indentation.
Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.
When invoking the Python 2 command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
Also see, this to check for mixing of tabs and spaces.
People using Sublime Text Editors,
Check this way.
- Select all the code.
- Select Tools.
- Click 'Install Package Control'.
- Then Click on Command Pallete.
- Search for 'Convert to Tabs' and select it.
- Done. All Spaces will be converted into Tabs.