使用SqlAlchemy时出错

时间:2018-08-29 09:49:31

标签: python postgresql sqlalchemy

 import os

 from sqlalchemy import create_engine
 from sqlalchemy.orm import scoped_session, sessionmaker

 engine = create_engine(os.getenv("DATABASE_URL")) 

 db = scoped_session(sessionmaker(bind=engine))

def main():
    flights = db.execute("SELECT origin, destination, duration FROM 
flights").fetchall()
 for flight in flights:
         print(f"{flight.origin} to {flight.destination}, {flight.duration} 
  minutes.")

if __name__ == "__main__":
main()

从命令行执行此代码时,出现以下错误

 File "list.py", line 6, in <module>
    engine = create_engine(os.getenv("DATABASE_URL"))
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\__init__.py", line 424, in create_engine
    return strategy.create(*args, **kwargs)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\strategies.py", line 52, in create
    plugins = u._instantiate_plugins(kwargs)
AttributeError: 'NoneType' object has no attribute '_instantiate_plugins'

我对此并不陌生,因此请忽略一些初学者的错误。如果你们能帮助我,我会很高兴。

我正在听讲座,讲师输入了相同的代码并使之运行。因此,请指导我我在这里犯了什么错误。

create_engine(os.getenv(“ DATABASE_URL”))计算为 (“ postgresql:// postgres:YAWER @ localhost:5432 / postgres”)

0 个答案:

没有答案