문제

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.

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top