Born 2 Code/Python
[파이썬] 모듈, 패키지 호출
yechoi
2020. 6. 16. 15:29
반응형
travel 이라는 디렉토리 아래에 thailand.py 등의 모듈이 있다고 하자.
#travel/thiland.py
class ThailandPackage:
def detail(self):
print("[태국 패키지 3박 5일] 방콕, 파타야 여행 50만원")
이 모듈을 호출하고 싶다면
import travel.thailand.ThailandPackage #폴더.py모듈.모듈 내 클래스
trip_to = travel.thailand.ThailandPackage()
trip_to.detail() #클래스 내 함수 사용
이밖에 가능한 형식
import travel.thailand #디렉토리 내 모듈 직접 호출
from travel.thailand import ThailandPackage #모듈에서 클래스 호출
from travel import thailand # 디렉토리에서 모듈 호출
주의,
from travel import *
위 같은 방식의 호출이 가능하려면, 패키지를 초기화해줘야 한다.
# travel/__init__.py
__all__ = ["thailand", "vietnam"] # 호출하고 싶은 모듈 나열
참고, 외부 함수 모듈 리스트
Python Module Index — Python 3.8.3 documentation
numbers Numeric abstract base classes (Complex, Real, Integral, etc.).
docs.python.org
반응형