Python AttributeError:“非类型”对象没有属性“\u实例化\u插件”(无法导入创建\u引擎)
回溯最近一次呼叫上次: 文件list.py,第6行,在 engine=create_engineos.getenvDATABASE_URL 文件C:\Users\Aakash\AppData\Local\Programs\Python\Python38-32\lib\site packages\sqlalchemy\engine\uuuu init\uuuu.py, 第479行,在创建引擎中 返回策略。创建*args,**kwargs 文件C:\Users\Aakash\AppData\Local\Programs\Python 38-32\lib\site packages\sqlalchemy\engine\strategies.py,第56行,在create中 plugins=u.\u实例化\u pluginskwargs AttributeError:“非类型”对象没有属性“\u实例化\u插件” 如果将我的代码更改为: 看起来os.getenvDATABASE_URL没有返回任何内容。调用create_engineOne会出现此错误。数据库URL是否在您的环境变量中定义?只需将其用作URL即可 Postgresql://username:password@主机:端口/数据库 直接将这些值传递到create_enginep中ostgresql://username:password@主机:端口/数据库 我也有同样的问题,现在它消失了,这对我很有效。唯一值得一提的是,在创建新用户和数据库并移动表之后,我得到了一个完全不同的错误。错误是' ModuleNotFoundError:没有名为“psycopg2”的模块 解决方案正在运行:pip3安装psycopg2二进制文件 PS:URL详细信息与您的详细信息。代替Python AttributeError:“非类型”对象没有属性“\u实例化\u插件”(无法导入创建\u引擎),python,cs50,Python,Cs50,回溯最近一次呼叫上次: 文件list.py,第6行,在 engine=create_engineos.getenvDATABASE_URL 文件C:\Users\Aakash\AppData\Local\Programs\Python\Python38-32\lib\site packages\sqlalchemy\engine\uuuu init\uuuu.py, 第479行,在创建引擎中 返回策略。创建*args,**kwargs 文件C:\Users\Aakash\AppData\Loca
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()
在url中键入以下内容:
engine=create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))
要避免在代码中键入postgresql连接(包括密码),请根据以下内容在终端中定义环境变量:
engine = create_engine("postgresql://scott:tiger@localhost/mydatabase")
db = scoped_session(sessionmaker(bind=engine))
也可以使用命令的简短形式:
source ~/.bash_profile
这将在当前shell中执行.bash_配置文件
有关重新加载.bash_配置文件的其他建议可以在评论中找到
. ~/.bash_profile