使用ipython-sql连接PostgreSQL失败,bind参数异常报错的排查与解决
ipython-sql连接PostgreSQL失败:
__init__() got an unexpected keyword argument 'bind' 问题场景
已有的PostgreSQL数据库可通过psql命令行、pgAdmin正常连接,但在JupyterLab、DataSpell等Python开发环境中使用ipython-sql魔法命令时,无法建立连接。
环境版本
- psycopg2:2.9.3
- SQLAlchemy:2.0.2
- ipython-sql:0.3.9
- PostgreSQL:15.3
执行代码
%load_ext sql %env DATABASE_URL=postgresql://retail_user:retail@localhost/retail_db %%sql SELECT * FROM orders LIMIT 10
报错内容
init() got an unexpected keyword argument 'bind'
Connection info needed in SQLAlchemy format, example: postgresql://username:password@hostname/dbname or an existing connection: dict_keys([])
解决方案
将SQLAlchemy版本降级至1.4.22,执行以下命令:
pip install sqlalchemy==1.4.22
问题原因
ipython-sql 0.3.9版本未适配SQLAlchemy 2.x的API变更,SQLAlchemy 2.x移除了部分旧接口(如bind参数相关的初始化逻辑),而旧版ipython-sql仍在调用这些废弃接口,导致参数不匹配错误。
内容的提问来源于stack exchange,提问作者Leslie
相关产品推荐
相关产品推荐

