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

[已解决]SQLAlchemy中导入枚举出错:ImportError: No module named enum

SQLAlchemy crifan 4749浏览 0评论

折腾:

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

期间参考:

Column and Data Types — SQLAlchemy 1.1 Documentation

flask – Defining SQLAlchemy enum column with Python enum raises “ValueError: not a valid enum” – Stack Overflow

去试试:

import enum
class NotificationTime(enum.Enum):
    NotNotify = “不提醒”
    WhenEventHappen = “事件发生时”
    FiveMinutesBefore = “5分钟前”
    FiftyMinutesBefore = “15分钟前”
    ThirtyMinutesBefore = “30分钟前”
    OneHourBefore = “1小时前”
    TwoHoursBefore = “2小时前”
    OneDayBefore = “1天前”
    TwoDaysBefore = “2天前”
    OneWeekBefore = “1周前”
class Event(db.Model):
    __tablename__ = “events”
    # Columns
    id = db.Column(db.Integer, primary_key = True, autoincrement = True, nullable=False)
    #user_openid = db.Column(db.String(64))
    #user_openid = db.Column(db.String(64), db.ForeignKey(‘user.openid’))
    user_openid = db.Column(db.String(64), db.ForeignKey(‘wechat_users.openid’))
    title = db.Column(db.String(128))
    start_date = db.Column(db.DateTime)
    end_date = db.Column(db.DateTime)
    location = db.Column(db.String(256))
    cur_user_num = db.Column(db.Integer)
    max_user_num = db.Column(db.Integer)
    is_public = db.Column(db.Boolean)
    description = db.Column(db.Text)
    notification_time = db.Column(db.Enum(NotificationTime))
    # joiners = db.relationship(‘EventJoiner’, backref=’user’, lazy=’dynamic’)
    #joiners = db.relationship(‘User’, secondary = event_joiners, backref=’events’)
    #joiners = db.relationship(‘User’, secondary=event_joiners, backref=’joined_events’,lazy=’dynamic’)
    joiners = db.relationship(‘User’, secondary=event_joiners, backref=’joined_events’)
    def __init__(self,
                 # id,
                 user_openid,
                 title = “”,
                 location = “”,
                 start_date = None,
                 end_date = None,
                 cur_user_num = 0,
                 max_user_num = 0,
                 is_public = False,
                 description = “”,
                 notification_time = NotificationTime.NotNotify):
        # self.id = id
        self.user_openid = user_openid
        self.title = title
        self.location = location
        self.start_date = start_date
        self.end_date = end_date
        self.cur_user_num = cur_user_num
        self.max_user_num = max_user_num
        self.is_public = is_public
        self.description = description
        self.notification_time = notification_time
    def __repr__(self):
        return u'<Event id=%d user_openid=%s title=%r>’ % (self.id, self.user_openid, self.title)

结果报错:

  File “/usr/share/nginx/html/SIPEvents/sipevents/models.py”, line 8, in <module>
    import enum
ImportError: No module named enum

把:

import enum

从:

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

移到:

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

结果错误依旧。

sqlalchemy ImportError: No module named enum

ImportError: No module named enum on python 2.7 · Issue #16 · numba/llvmlite

->需要依赖(模拟python 3.4中的?)enum34

-》pip install enum34

python 2.7 – ImportError: No module named enum – Stack Overflow

之前就看到了:

spoqa/sqlalchemy-enum34: SQLAlchemy type to store standard enum.Enum values

enum34 1.1.6 : Python Package Index

Dependency missing: “ImportError: No module named enum” · Issue #67 · poppy-project/pypot

(SIPEvents) ➜  SIPEvents pip install enum34
Collecting enum34
  Downloading enum34-1.1.6-py2-none-any.whl
Installing collected packages: enum34
Successfully installed enum34-1.1.6

然后再去试试,就可以了。

[总结]

SQLAlchemy中的enum枚举类型,依赖于Python的enum枚举

而Python中的enum是在Python 3.4之后才支持

然后被向后移至,支持了之前的版本,包括Python 2.7版本

对应的,对应的enum包叫做:enum34

安装办法:

pip install enum34

即可解决此:

ImportError: No module named enum

的问题。

转载请注明:在路上 » [已解决]SQLAlchemy中导入枚举出错:ImportError: No module named enum

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.204 seconds, using 22.17MB memory