且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 sqlalchemy 连接到 postgresql 时出错

更新时间:2022-11-30 15:34:55

os.getenv 用于获取环境变量的值,默认返回None如果该变量不存在.您将连接字符串传递给它,该字符串(几乎可以肯定)不作为环境变量存在.所以它返回 None,它被提供给 create_engine,它失败了,因为它需要一个连接字符串.只需直接传递您的连接字符串:

os.getenv is used to get the value of an environment variable, and returns None by default if that variable doesn't exist. You're passing it your connection string, which (almost certainly) doesn't exist as an environment variable. So it's returning None, which is given to create_engine, which fails because it's expecting a connection string. Just pass your connection string in directly:

engine = create_engine("postgresql://postgres:nehal@localhost:5432/lecture3")