Python - 在我的新 Mac book M3 上创建新虚拟机后,未定义回溯、记录器和 SQLAlchemy

问题描述 投票:0回答:1

有人能帮我解决未找到课程的问题吗?我确实导入了这些包,但它一直抱怨类未定义。请指教。

  • 名称错误:名称“回溯”未定义
  • 名称错误:名称“记录器”未定义
  • 名称错误:名称“SQLAlchemy”未定义

在我的新 Macbook M3 pro 上安装 Flask 后,我得到了上述“类”未定义的信息。我的 Python 网站曾经可以正常工作。正如我导入这些包一样。请参阅以下代码:


    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


the (.venv) directory contains the following files
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

and the bin directory:
(.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/[email protected]/bin/python3.11


python-3.x logging flask-sqlalchemy
1个回答
0
投票

对于回溯和记录器,我已经解决了问题。但对于 NameError: name 'SQLAlchemy' is not Defined,我仍然不知道如何解决该问题。

我确实跑了 pip3 安装flask_sqlalchemy

在我的Python代码中:

from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()  <=== it complain SQLAlhemy is not defined

class UserProfile(db.Model):
    __tablename__ = 'user_profile'

    user_id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(64))
    last_name = db.Column(db.String(64))
    faked_name = db.Column(db.String(64))
    subscription_date = db.Column(db.String(64))
    status =  db.Column(db.String(64))
    created_date = db.Column(db.String(64))
    trading_experience = db.Column(db.String(64))
    mobile_number = db.Column(db.String(64))

    def __init__(self, user_id, first_name, last_name, faked_name, subscription_date, status, created_date, trading_experience, mobile_number):
        self.user_id = user_id
        self.first_name = first_name
        self.last_name = last_name
        self.faked_name = faked_name
        self.subscription_date = subscription_date
        self.status = status
        self.created_date = created_date
        self.trading_experience = trading_experience
        self.mobile_number = mobile_number
© www.soinside.com 2019 - 2024. All rights reserved.