[python] package 만들고 발생되는 에러 확인하기

hamingu·2021년 4월 20일
0

python package를 만들고 발생되는 에러를 확인해보자.

python package 만들기

main.py에서 상대 경로로 add_and_multiply를 임포트 했을 때 발생하는 에러

에러 로그
'Attempted relative import beyond top-level package'

python 공식 문서에서 확인한 import 방식

Note that relative imports are based on the name of the current module. Since the name of the main module is always "main", modules intended for use as the main module of a Python application must always use absolute imports.

해석
상대경로로 import 시 현재 module의 이름을 기반으로 한다다. main 모듈의 이름은 항상 "main"이기 때문에, 파이썬 어플리케이션은 항상 절대경로를 사용해야 한다.

절대경로로 설정 후 import 시 문제없이 작동.

__init__.py 용도

  • __init__.py 파일은 해당 디렉토리가 패키지의 일부임을 알려주는 역할을 한다.
  • 초기의 __init__.py은 빈 파일이지만 package 실행 시 초기 설정을 주고 싶다면 이 파일에 초기 설정에 관련된 코드들을 작성해주면 된다.
  • profile
    프로그래밍구

    0개의 댓글