Search code examples
pandassqlalchemy

Pandas.read_sql throw exception from sqlalchemy: AttributeError: 'Connection' object has no attribute 'exec_driver_sql'


#pd.version is '2.0.2' #sqlalchemy.version is '1.3.7'

import pandas as pd 
from sqlalchemy import create_engine

conn_str = 'connection_string'
engine = create_engine(conn_str)

Yesterday everything was fine and worked but today, when I call pd.read_sql(ANY_QUERY, con=engine) I get an exception:

~/anaconda3/lib/python3.9/site-packages/pandas/io/sql.py in read_sql(sql, con, index_col, coerce_float, params, parse_dates, columns, chunksize, dtype_backend, dtype)
        661             )
        662         else:
    --> 663             return pandas_sql.read_query(
        664                 sql,
        665                 index_col=index_col,
    
    ~/anaconda3/lib/python3.9/site-packages/pandas/io/sql.py in read_query(self, sql, index_col, coerce_float, parse_dates, params, chunksize, dtype, dtype_backend)
       1736 
       1737         """
    -> 1738         result = self.execute(sql, params)
       1739         columns = result.keys()
       1740 
    
    ~/anaconda3/lib/python3.9/site-packages/pandas/io/sql.py in execute(self, sql, params)
       1560         args = [] if params is None else [params]
       1561         if isinstance(sql, str):
    -> 1562             return self.con.exec_driver_sql(sql, *args)
       1563         return self.con.execute(sql, *args)
       1564 
    
    AttributeError: 'Connection' object has no attribute 'exec_driver_sql'

The solution is to add to engine .raw_connection() -> engine.raw_connection()

But what is happening? I did nothing, no new libs, no lib updates. Could anyone please explain me.


Solution

  • I've just had the same problem as you. This is how I'm informed: Pandas requires version 1.4.16 or newer of sqlalchemy (version 1.3.24 currently installed) Maybe upgrading your sqlalchemy version may help.