Frage

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.

War es hilfreich?

Lösung

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top