ImportError: cannot 导入 name 'X' 从 'Y'
错误信息
cannot 导入 name 'sleep' 从 '时间'
快速摘要
你 tried 到导入 name 从模块, 但 name doesn't exist in 那个模块 or 那里's circular 导入.
为什么发生
name doesn't exist in 模块 (typo or 错误模块)
Circular 导入 — two 模块导入每个 other
模块 exists 但特定 name 是移除 or renamed
相对导入 issues in 包
最小示例
✗ 错误代码
from time import sleepp # ImportError — typo in 'sleep'✓ 修复代码
from time import sleep # Correct name
# or
import time
time.sleep(1)name 'sleepp' doesn't exist in 时间模块. Fix typo 到 'sleep'.
如何诊断
检查模块's 内容使用 dir(模块) or documentation
验证 name spelling
Look for circular 导入模式
如何修复
Fix 导入到 use 正确 name
Restructure code 到 avoid circular 导入
Use 完整模块 path 相反的 从...导入
导入内部函数到 break circular dependencies
如何预防
Use IDE autocomplete 到验证导入 names
结构模块到 avoid circular dependencies
Keep 导入 at 顶部的 file