1

I used 2to3 to convert a folder of python modules. Everything went smooth, but when I went to run some of them it gave me an error about spaces and tabs. My theory: when 2to3 changes a line it uses tabs and not spaces unlike the rest of the non-changed lines. I was wondering if there was a way to change this.

The error is:

TabError: inconsistent use of tabs and spaces in indentation

The code snippet is difficult to show because a tab is shown equivalent to 4 spaces, but I checked and this was happening.

gypaetus
  • 6,873
  • 3
  • 35
  • 45
carloabelli
  • 4,289
  • 3
  • 43
  • 70
  • Could you show the specific error - and a snippet of code relating to that area of a file? – Jon Clements Jan 15 '13 at 18:09
  • 3
    Please do include the error message you got. Are you saying your input files had only tabs and 2to3 used spaces for indentation, causing the end result to be mixed? – Martijn Pieters Jan 15 '13 at 18:09
  • 1
    python -tt" & reindent.py – root Jan 15 '13 at 18:12
  • My original used spaces and 2to3 used tabs – carloabelli Jan 15 '13 at 18:18
  • 1
    It's extremely, extremely, extremely hard to believe that 2to3 actually *inserted* tabs. Spaces have been **overwhelmingly** preferred over tabs in the Python community since almost forever, long before there was even the idea to make a Python 3. So the idea that a tool *provided with the official Python*, and *written by developers of the official Python* would actually go against this is practically incomprehensible. – John Y Nov 25 '15 at 23:05

1 Answers1

7

2to3 should not replace white space with tabs so I am guessing that you get a TabError because those tabs where already present in the code and python 3 does not allow mixing tabs and spaces within a single file whereas that is fine in python 2.

This can be fixed using the reindent.py script that comes with python and can be found in linux systems under /usr/lib/python2.7/Tools/scripts/reindent.py or optionally can be installed using pip by pip install reindent. Then the command to replace tabs with spaces is:

reindent.py file.py
gypaetus
  • 6,873
  • 3
  • 35
  • 45