main module, package module, __init__.py

강민성·2021년 12월 3일
0

main module에서 package module을 import하는 방법

절대경로로 import해야 한다.
상대경로를 사용할 시 ImportError가 발생한다.
이유:

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.

package에서 __init__.py 파일의 역할

  • __init__.py 파일이 있는 디렉토리가 package라는 것을 나타냄
  • __init__.py 파일을 사용하여 사용하고자 하는 함수의 경로 단축 가능
  • __init__.py 파일 안에 __all__ 변수를 써서 사용되는 숨기고 싶은 함수를 숨길 수 있음
profile
Back-end Junior Developer

0개의 댓글