IndentationError: unindent does not match any outer indentation level

时间:2025-03-22 12:05:19

【problem】

  从别处copy过来的python代码经过自己改动后,运行出错

【解决过程】

  vim file :set list  # cat -A file 也可以

  IndentationError: unindent does not match any outer indentation level

  可以看到9-12行的indentaion是TAB,13行是4个空格。

  Python语法不支持代码对齐中混用TAB和空格。

  vim file :1,$s/\t/    /g  # 将TAB换成4个空格

  ps: 为了代码风格的统一,最好将TAB替换成4个空格

  vim ~/.vimrc

    set expandtab