我跟着这个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。