AttributeError: ‘OptionEngine‘ object has no attribute ‘execute‘

背景

pandas:1.5.2
sqlalchemy:2.0.4


import pandas as pd
from sqlalchemy 
import create_engine # SQLAlchemy v. 2.0!

sql_engine = create_engine('postgresql://user:pass@host:5432/database')

df = pd.read_sql(f'''    SELECT * FROM table LIMIT 100''',
    sql_engine)

报错


Traceback (most recent call last):
   File "/app/test.py", line 31, in <module>
     df = pd.read_sql(f'''
  File "/usr/local/lib/python3.10/site-packages/pandas/io/sql.py", line 590, in read_sql
     return pandas_sql.read_query(
   File "/usr/local/lib/python3.10/site-packages/pandas/io/sql.py", line 1560, in read_query
     result = self.execute(*args)
   File "/usr/local/lib/python3.10/site-packages/pandas/io/sql.py", line 1405, in execute
     return self.connectable.execution_options().execute(*args, **kwargs)
 AttributeError: 'OptionEngine' object has no attribute 'execute'

解决

https://github.com/pandas-dev/pandas/issues/40686

在这篇文章中看到,sqlalchemy的1.4和2.0两种不同的语法,pandas暂时还不支持2.0,所以将sqlalchemy回退到1.4.45即可。

你可能感兴趣的:(python,pandas,python,开发语言)