You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacBook M3虚拟机中traceback/logger/SQLAlchemy未定义问题求助

类未定义排查求助

我在新MacBook M3 Pro上安装Flask后,原本正常运行的Python网站出现多个NameError,明明已经导入对应包,但系统持续提示类/模块未定义,求指导。

出现的错误

  • NameError: name 'traceback' is not defined
  • NameError: name 'logger' is not defined
  • NameError: name 'SQLAlchemy' is not defined

相关代码片段

import sys, traceback
from loguru import logger
from sqlalchemy import SQLAlchemy

except Exception as exc:
    print(exec)
    traceback.print_exception(exc)  # <== 提示'traceback' is not defined

# configure logger
logger.add("static/job.log", format="{time} - {message}")  # <= logger is not defined
PROJECT_ROOT_DIR = Path(__file__).parent.parent
db_file = str(PROJECT_ROOT_DIR) + '/db.sqlite3'

db = SQLAlchemy()         # <== SQLAlchemy is not defined

虚拟环境信息

.venv目录结构

drwxr-xr-x  21 kamlau  staff  672 24 Dec 23:03 bin
drwxr-xr-x   3 kamlau  staff   96 24 Dec 22:58 include
drwxr-xr-x   3 kamlau  staff   96 24 Dec 22:58 lib
-rw-r--r--  1 kamlau  staff  325 24 Dec 22:58 pyvenv.cfg

bin目录内容

(.venv) kamlau@Kams-MacBook-Pro bin % ls -l
total 144
-rw-r--r--  1 kamlau  staff  9033 24 Dec 22:58 Activate.ps1
-rw-r--r--  1 kamlau  staff  2023 24 Dec 22:58 activate
-rw-r--r--  1 kamlau  staff   949 24 Dec 22:58 activate.csh
-rw-r--r--  1 kamlau  staff  2229 24 Dec 22:58 activate.fish
-rwxr-xr-x  1 kamlau  staff   266 24 Dec 23:03 alembic
-rwxr-xr-x  1 kamlau  staff   260 24 Dec 23:03 dotenv
-rwxr-xr-x  1 kamlau  staff   267 24 Dec 23:03 email_validator
-rwxr-xr-x  1 kamlau  staff   261 24 Dec 23:03 flask
-rwxr-xr-x  1 kamlau  staff   270 24 Dec 23:03 gunicorn
-rwxr-xr-x  1 kamlau  staff   267 24 Dec 23:03 htmlmin
-rwxr-xr-x  1 kamlau  staff   266 24 Dec 23:03 jsonschema
-rwxr-xr-x  1 kamlau  staff   274 24 Dec 23:03 lesscpy
-rwxr-xr-x  1 kamlau  staff   266 24 Dec 23:03 mako-render
-rwxr-xr-x  1 kamlau  staff   274 24 Dec 22:58 pip
-rwxr-xr-x  1 kamlau  staff   274 24 Dec 22:58 pip3
-rwxr-xr-x  1 kamlau  staff   274 24 Dec 22:58 pip3.11
lrwxr-xr-x  1 kamlau  staff    10 24 Dec 22:58 python -> python3.11
lrwxr-xr-x  1 kamlau  staff    10 24 Dec 22:58 python3 -> python3.11
lrwxr-xr-x  1 kamlau  staff    44 24 Dec 22:58 python3.11 -> /opt/homebrew/opt/python@3.11/bin/python3.11

排查与解决步骤

1. 修复代码语法错误

代码中except语句没有对应的try块,这会破坏整个代码的语法结构,导致Python无法正确解析前面的导入语句。

  • 处理方式:给except添加对应的try块,或者删除多余的except语句(如果是代码片段粘贴时的遗漏)。

2. 确认虚拟环境已激活

运行代码前必须确保激活了.venv:

  • 终端执行:source .venv/bin/activate
  • 激活后终端提示符会显示(.venv),再启动Flask项目。

3. 验证依赖包安装状态

激活虚拟环境后,执行pip list检查loguru、sqlalchemy、flask是否存在。如果缺失,执行:

pip install loguru sqlalchemy flask

4. 确认Python解释器路径

确保运行代码的解释器是虚拟环境内的版本:

  • 激活环境后执行which python,输出应为./.venv/bin/python
  • 如果用IDE运行,需在设置中切换到虚拟环境的Python路径。

5. 修正代码笔误

代码中print(exec)是变量名错误,应改为print(exc)。

内容的提问来源于stack exchange,提问作者mylottofinder.com.au

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 15:21:05