最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Flask的SQLAlchemy中无法导入Enum变量:ImportError: cannot import name NotificationTime

Flask crifan 4038浏览 0评论

折腾:

[已解决]SQLAlchemy中添加枚举类型字段

期间,用代码:

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/db_create.py

from sipevents import User, Event
# from models import  NotificationTime
from sipevents import  NotificationTime

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/models.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from sipevents import app
from . import db
# from app import db
# from datetime import datetime
import enum
event_joiners = db.Table(‘event_joiners’,
    db.Column(‘event_id’, db.Integer, db.ForeignKey(‘events.id’)),
    db.Column(‘joiner_openid’, db.Integer, db.ForeignKey(‘wechat_users.openid’))
)
class User(db.Model):
    __tablename__ = ‘wechat_users’
   。。。
class NotificationTime(enum.Enum):
    NotNotify = u"不提醒"
    WhenEventHappen = u"事件发生时"
    FiveMinutesBefore = u"5分钟前"
    FiftyMinutesBefore = u"15分钟前"
    ThirtyMinutesBefore = u"30分钟前"
    OneHourBefore = u"1小时前"
    TwoHoursBefore = u"2小时前"
    OneDayBefore = u"1天前"
    TwoDaysBefore = u"2天前"
    OneWeekBefore = u"1周前"
class Event(db.Model):
    __tablename__ = "events"
 。。。

文件结构是:

结果报错:

  File "db_create.py", line 9, in <module>
    from sipevents import  NotificationTime
ImportError: cannot import name NotificationTime

sqlalchemy enum ImportError: cannot import name

python – "from sqlalchemy import event" not working – Stack Overflow

python – SQLAlchemy: ImportError: cannot import name Column – Stack Overflow

Flask  cannot import enum

SQLAlchemy Enum column type using python’s enum34

 换成:

from models import  NotificationTime

结果:

  File "db_create.py", line 9, in <module>
    from models import  NotificationTime
ImportError: No module named models

最后的最后,用:

from sipevents.models import  NotificationTime

才把sipevents下面的models.py中的enum类型的:NotificationTime

导入进来

才可以正常运行。

[总结]

此处,对于如下的目录结构:

sipevents

    models.py

db_create.py

其中:

models.py中,有对应的enum类型的NotificationTime,以及对应的表User和Event

db_create.py中,之前导入Table的User和Event,只需要:

from sipevents import models
from sipevents import User, Event

但是想要去导入models.py中对应的枚举:NotificationTime

不能是:

from sipevents import  NotificationTime

也不能是:

from models import  NotificationTime

却只能是:

from sipevents.models import  NotificationTime

否则都会出现导入出错。

至于根本原因,暂时没搞懂。

没搞懂,为何:

from sipevents import  NotificationTime

(在已经

from sipevents import models

了,用)

from models import  NotificationTime

无法正常导入。

转载请注明:在路上 » [已解决]Flask的SQLAlchemy中无法导入Enum变量:ImportError: cannot import name NotificationTime

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.184 seconds, using 22.11MB memory