reindent.py - 无法从命令行运行
问题描述:
我在Python中遇到了缩进问题。所以我下载reindent.py
来纠正缩进错误。reindent.py - 无法从命令行运行
我使用下面的命令 - 安装reindent.py:
pip install reindent
但我在命令行中运行它让我看到下面的错误 - :
Traceback (most recent call last):
File "/usr/local/bin/reindent", line 3, in <module>
main()
File "/usr/local/lib/python2.7/dist-packages/reindent.py", line 92, in main
check(arg)
File "/usr/local/lib/python2.7/dist-packages/reindent.py", line 118, in check
if r.run():
File "/usr/local/lib/python2.7/dist-packages/reindent.py", line 177, in run
tokenize.tokenize(self.getline, self.tokeneater)
File "/usr/lib/python2.7/tokenize.py", line 170, in tokenize
tokenize_loop(readline, tokeneater)
File "/usr/lib/python2.7/tokenize.py", line 176, in tokenize_loop
for token_info in generate_tokens(readline):
File "/usr/lib/python2.7/tokenize.py", line 357, in generate_tokens
("<tokenize>", lnum, pos, line))
File "<tokenize>", line 127
for w in transcript:
^
IndentationError: unindent does not match any outer indentation level
我与它运行以下命令 - :
reindent -n test1.py
我以为reindent
应该纠正日错误不会告诉我他们在哪里发生。
答
reindent.py
将制表符更改为空格,并可以使不规则的缩进统一为4个空格。它不会尝试捕捉或修复IndentationError
s。
考虑以下代码它有一个IndentationError:
def foo():
print("Let's go")
for i in range(2): <-- IndentationError
print('Peay')
它会产生类似的错误消息,你所得到的一个:
% reindent.py script.py
Traceback (most recent call last):
...
File "/usr/lib/python2.7/tokenize.py", line 170, in tokenize
tokenize_loop(readline, tokeneater)
File "/usr/lib/python2.7/tokenize.py", line 176, in tokenize_loop
for token_info in generate_tokens(readline):
File "/usr/lib/python2.7/tokenize.py", line 357, in generate_tokens
("<tokenize>", lnum, pos, line))
File "<tokenize>", line 9
for i in range(2):
^
IndentationError: unindent does not match any outer indentation level
两个
def foo():
print("Let's go")
for i in range(2):
print('Peay')
和
def foo():
print("Let's go")
for i in range(2):
print('Peay')
是有效的方法来修复代码。 reindent.py
(或其 依赖的tokenize
模块)不会尝试猜测编码器打算使用哪一个模块。因此, IndentationErrors
是SyntaxError
,其至少有时需要人类干预来修复。
'test1.py'中有一个IndentationError。修复IndentationError,然后'reindent.py'将会起作用。 – unutbu
@unutbu不是reindent.py假设为我修复缩进错误?有什么办法可以自动修复缩进错误吗? – Machina333