AttributeError:__ exit__当使用`with`语句时

时间:2014-11-17 05:04:31

标签: python exception sqlalchemy yield with-statement

我跟着这个guide为SQLAlchemy写了一个session_scope。当我运行代码时,我收到AttributeError例外:

Traceback (most recent call last):
  File "/Users/tuanchauict/workspace/opla-manga/python-server-non-django/demo.py", line 56, in <module>
    with session_scope() as s:
AttributeError: __exit__

我的代码是:

Session = sessionmaker(bind=engine, autocommit=False, autoflush=False)

def session_scope():
    session = Session()
    try:
        yield session
        session.commit()

    except Exception as e:
        session.rollback()
    finally:
        session.close()

with session_scope() as session:  # <=== error here
    pass

我在Mac OS 10.10上运行Python 3.4。

0 个答案:

没有答案