Python报错 TabError: inconsistent use of tabs and spaces in indentation

一、出现原因

在修改python文件的时候,运行程序出现报错: TabError: inconsistent use of tabs and spaces in indentation

出现这个错误是因为笔者在notepad++ 遇到的都是看似有空格但实际没有空格引起的。
打开notepad++ 显示空格选项就知道这个位置是不是真的有空格了。

二、notepad++ 显示 空格

在notrpad++中打开显示空格选项:
Python报错 TabError: inconsistent use of tabs and spaces in indentation

Python报错 TabError: inconsistent use of tabs and spaces in indentation

Python报错 TabError: inconsistent use of tabs and spaces in indentation
从上图可以发现,val_m 前面并不是空格。
主要原因是制表符Tab 并不是 四个空格 而出现的问题。

三、解决方法

解决方法:设置–>首选项,然后找到语言,勾选右下角的 替换为表格,这样就可以保证 一个 Tab键 = 4个空格。
Python报错 TabError: inconsistent use of tabs and spaces in indentation
Python报错 TabError: inconsistent use of tabs and spaces in indentation

然后就可以同时使用 Tab键 和空格了。

参考链接:
[1] Python中常出现TabError: inconsistent use of tabs and spaces in indentation错误解决方法 https://blog.****.net/qq_41096996/article/details/85947560
[2] 把Notepad++的tab设置为四个空格 https://www.cnblogs.com/jyfootprint/p/9409934.html