Files
KCGL/inventory-backend/find_error.py

50 lines
1.9 KiB
Python

import os
def find_bad_imports(root_dir):
# 我们要查找的错误引用字符串
targets = [
"app.models.material",
"from app.models.material"
]
print(f"🚀 开始扫描目录: {os.path.abspath(root_dir)}")
print(f"🔍 正在寻找残留代码: {targets} ...\n")
found_count = 0
# 排除的目录(避免扫描虚拟环境或缓存)
exclude_dirs = {'.git', '__pycache__', 'venv', '.idea', '.vscode'}
for root, dirs, files in os.walk(root_dir):
# 修改 dirs 列表以跳过排除的目录
dirs[:] = [d for d in dirs if d not in exclude_dirs]
for file in files:
if file.endswith(".py"):
file_path = os.path.join(root, file)
try:
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
for i, line in enumerate(lines, 1):
for target in targets:
if target in line:
print(f"❌ 发现错误文件: {file_path}")
print(f" ➡️ 第 {i} 行: {line.strip()}")
found_count += 1
except Exception as e:
# 忽略无法读取的文件(如二进制文件)
pass
print("-" * 50)
if found_count == 0:
print("✅ 恭喜!未发现任何 'app.models.material' 的残留引用。")
print("💡 如果依然报错,请尝试完全重启 Docker 或清理 __pycache__。")
else:
print(f"⚠️ 共发现 {found_count} 处错误引用,请照上方路径逐一修改。")
print("👉 修改方法: 将 'app.models.material' 改为 'app.models.base'")
if __name__ == "__main__":
# 扫描当前目录
find_bad_imports(".")