본문 바로가기
DevLog/오류 해결 (Troubleshooting)

[Python] AttributeError: 'OptionEngine' object has no attribute 'execute'

by Coding_WONI 2024. 5. 29.

최근 프로젝트에서 SQLAlchemy와 Pandas를 사용해 PostgreSQL 데이터베이스에 연결하고 데이터를 가져오는 작업을 진행하던 중 다음과 같은 코드를 실행하면서 문제가 발생했습니다:

from sqlalchemy import create_engine
import pandas as pd

# PostgreSQL 데이터베이스 연결 정보 (예시)
username = 'postgres'
password = '1234'
hostname = 'localhost'
database_name = 'postgres'
port = '5432' 

# SQLAlchemy 엔진 생성
engine = create_engine(f'postgresql://{username}:{password}@{hostname}:{port}/{database_name}')

query = "SELECT * FROM table;"

result_df = pd.read_sql_query(sql=query, con=engine)

 

코드를 실행하니 아래와 같은 오류 메시지가 나타났습니다

 

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

 

데이터베이스에 데이터가 잘 들어간 것을 확인했고, 과거에 잘 작동했던 코드라 당황스러웠습니다. 여러 가지 방법으로 문제를 해결하려고 시도한 끝에, 결국 문제의 원인은 Pandas 버전 오류임을 알게 되었습니다.

문제 해결 방법

만약 여러분도 같은 오류를 마주쳤다면, 다음과 같이 Pandas와 SQLAlchemy 버전을 확인하고 업데이트해 보세요. 이로 인해 오류 없이 코드가 잘 작동할 것입니다.