SkavinTheRat

Back

异常典型信息具体原因
EOFErrorEOF when reading a line需要读一行,但流已经结束,什么都没读到。input() 最常见。
pickle 的 EOFErrorRan out of inputpickle 要读完整对象,空流或数据不完整。截断数据还可能报 UnpicklingError: pickle data was truncated。
marshal 的 EOFErrorEOF read where object expectedmarshal 要读一个完整对象,但流提前结束。
json.JSONDecodeErrorExpecting value: line 1 column 1 (char 0)JSON 输入为空,或格式不完整。空文件、空字符串都会这样。
FileNotFoundError[Errno 2] No such file or directory: 'a.txt'路径不存在,或拼写、工作目录不对。属于 OSError。
PermissionError[Errno 13] Permission denied: 'a.txt'没有读、写或执行权限,文件也可能被占用。
FileExistsError[Errno 17] File exists: 'a.txt'创建文件或目录时目标已存在,例如 os.mkdir。
IsADirectoryError[Errno 21] Is a directory: 'folder'把目录当文件打开。
NotADirectoryError[Errno 20] Not a directory: 'a.txt/x'路径中间把文件当成了目录。
OSError[Errno 28] No space left on device操作系统级错误的总类,磁盘满、文件描述符耗尽等。
BlockingIOError[Errno 11] Resource temporarily unavailable非阻塞 I/O 暂时无法完成。
InterruptedError[Errno 4] Interrupted system call系统调用被信号打断。
ProcessLookupError[Errno 3] No such process操作的目标进程不存在。
TimeoutError[Errno 110] Connection timed out 或 timed out网络或 I/O 超时,消息随平台和场景变化。
ConnectionRefusedError[Errno 111] Connection refused目标端口没有服务监听。
ConnectionResetError[Errno 104] Connection reset by peer对端强制断开连接。
BrokenPipeError[Errno 32] Broken pipe管道另一端已关闭,常见于 head、less 提前退出。
UnicodeDecodeError'utf-8' codec can't decode byte 0xff in position 0: invalid start byte用错误编码读取字节,或文件本身不是该编码。
UnicodeEncodeError'ascii' codec can't encode characters ...: ordinal not in range(128)把非 ASCII 文本写入只接受 ASCII 的目标。
异常典型信息具体原因
ModuleNotFoundErrorNo module named 'foo'按 sys.path 搜索后没找到模块,属于 ImportError 子类。
ImportErrorcannot import name 'bar' from 'foo'模块找到了,但里面没有这个名字,常见于循环导入。
ImportErrorattempted relative import with no known parent package直接运行包内文件,缺少包上下文。
NameErrorname 'x' is not defined运行到该名字时,局部、闭包、全局和内置里都找不到它。
UnboundLocalErrorcannot access local variable 'x' where it is not associated with a value函数里某处给 x 赋值,于是 x 被视为局部变量,但读取发生在赋值之前。
AttributeError'str' object has no attribute 'append'对象类型根本没有这个属性,或属性名拼错。
TypeErrorunsupported operand type(s) for +: 'int' and 'str'操作符用在了不兼容的类型上。
TypeError'int' object is not callable把不可调用的对象当函数调用。
TypeErrorf() missing 1 required positional argument: 'x'函数调用缺少参数,或多给了参数。
ValueErrorinvalid literal for int() with base 10: 'abc'类型正确但值不合法,int("abc") 就是典型例子。
ValueErrortoo many values to unpack (expected 2)解包的元素个数和变量个数不匹配。
IndexErrorlist index out of range序列下标越界,负下标越界也算。
KeyError'name'字典里没有这个键。KeyError 的消息通常是键的 repr。
ZeroDivisionErrordivision by zero分母为零。整除和取模的消息可能是 integer division or modulo by zero、integer modulo by zero。
AssertionErrorAssertionError 或自定义消息assert 条件为假。注意 python -O 会跳过断言,不适合做用户输入校验。
NotImplementedErrorNotImplementedError抽象方法或暂未实现的分支。它属于 RuntimeError。
RecursionErrormaximum recursion depth exceeded递归超过 sys.getrecursionlimit(),通常是无限递归。
MemoryErrorMemoryError,往往没有消息内存不够,常见于一次性构造超大列表或字符串。
SyntaxErrorinvalid syntax解析阶段语法错误,通常发生在导入或编译文件时,不是运行时。
IndentationErrorexpected an indented block after function definition on line 3缩进结构不合法,属于 SyntaxError。
TabErrorinconsistent use of tabs and spaces in indentation同一文件混用 Tab 和空格,属于 IndentationError。
RuntimeErrordictionary changed size during iteration运行时状态不一致,最典型是遍历字典时修改字典。
StopIterationStopIterationnext() 迭代器耗尽。若在生成器内部逃出,会被转换成 RuntimeError: generator raised StopIteration。
ReferenceErrorweakly-referenced object no longer exists使用 weakref 代理时,原对象已经被回收。
BufferErrorExisting exports of data: object cannot be re-sized对象已导出缓冲区,不能被重新调整大小。
SystemError各种内部错误信息通常不是用户代码逻辑问题,可能是 C 扩展或解释器内部错误。
Python 常见异常与报错信息速查
https://skavintherat.vercel.app/blog/python-error-reference
Author SkavinTheRat
Published at 2026年10月15日