본문 바로가기

Python

[python] 디버깅하기 & Error난 줄 찾기 (feat. traceback)

반응형

보통 python 디버깅할때  아래처럼 몇 라인에 있는지 확인하고 디버깅을 많이 하게 된다.

Traceback (most recent call last):
  File "test.py", line 220, in <module>
    test(start_date)
  File "test.py", line 204, in test
    con.close()
UnboundLocalError: local variable 'con' referenced before assignment

 

그런데 내가 디버깅하는데 아래와 같이 아무런 설명 없이 에러가 뜨는것이다.

error <class 'TypeError'> execute() takes from 2 to 3 positional arguments but 4 were given

그래서 찾아봤더니 traceback 줄이 어디서 에러가 났는지 확인해 준다고 한다.

import traceback

try:
    # 여기에 코드를 작성합니다.
    pass
except Exception as e:
    traceback.print_exc()
import traceback

def some_function():
    # 여기에 코드를 작성합니다.
    raise ValueError("Something went wrong.")

try:
    some_function()
except Exception as e:
    print("An error occurred:")
    print(traceback.format_exc())
반응형