-
[python]2. Module and Project인공지능/부스트캠프 Ai Tech 2022. 1. 21. 18:26728x90
[python]2. Module and Project In [1]:from IPython.core.display import display, HTML display(HTML("<style>.container { width:90% !important; }</style>")) #창 맞추기위함
모듈 만들기¶
파이썬의 Module은 py 파일을 의미
같은 폴더에 Module에 해당하는 .py 파일과 사용하는 .py을 저장한 후
import 문을 사용해서 module을 호출
In [ ]:# fah_converter.py로 저장 def convert_c_to_f(celcius_value): return celcius_value * 9.0 / 5 + 32
In [2]:import fah_converter # fah.converter.py의 모든 코드가 메모리에 로딩 fah_converter.convert_c_to_f(42.6) # 모듈이름.함수명
Out[2]:108.68
In [ ]:# module_ex.py import fah_converter if __name__ == "__main__": print("Enter a celcius value : ") celcius = float(input()) fah = fah_converter.convert_c_to_f(celcius) print("That's {0} degrees Fahrenheit".format(fah))
pychche : 메모리에 모듈을 불러 올때 더 빨리 컴파일을 위해 캐싱을 미리해 놓은 폴더
In [2]:# Alias 설정하기 - 모듈명을 별칭으로 써서, as import fah_converter as fah # as fah , 어디서 왔는지 확인 가능 print(fah.convert_c_to_f(41.6))
106.88000000000001
In [3]:# 모듈에서 특정 함수 또는 클래스만 호출하기 from import from fah_converter import convert_c_to_f print(convert_c_to_f(41.6)) # 어디서 왔는지 알기 어려워 추천 안함
106.88000000000001
In [4]:# 모듈에서 모든 함수 또는 클래스를 호출하기 from fah_converter import * print(convert_c_to_f(41.6))
106.88000000000001
Built-in Modules¶
파이썬이 기본 제공하는 라이브러리, 문자처리, 웹, 수학 등 다양한 모듈이 제공, 별다른 조치없이 import 문으로 활용 가능
In [5]:import random random.randint(1,100)
Out[5]:74
In [1]:import time time.localtime()
Out[1]:time.struct_time(tm_year=2022, tm_mon=1, tm_mday=10, tm_hour=14, tm_min=25, tm_sec=24, tm_wday=0, tm_yday=10, tm_isdst=0)
폴더명으로 실행하기¶
python 폴더명 으로 실행하기 위해서는, 폴더의 루트에 main 파일을 만들어 실행한다.
In [6]:# game 폴더의 __main__.py 파일 if __name__ == '__main__': print("Hello Game")
Hello Game
__all__
: 앞으로 쓸 모듈들의 이름(폴더명)을 작성[[참고]] package namesapce¶
Package 내에서 다른 폴더의 모듈을 부를때 상대 참조로 호출하는 방법
In [ ]:from game.graphic.render import render_test # 절대참조 from .render import render_test # .현재 디랙토리 기준 from ..sound.echo import echo_test # .. 부모 디렉토리 기준
'인공지능 > 부스트캠프 Ai Tech' 카테고리의 다른 글
[python]5. Numpy (0) 2022.01.21 [python]4. Python Data Handling (0) 2022.01.21 [python]3. Exception_File_LogHandling (0) 2022.01.21 [python]0. Pythonic code (0) 2022.01.21 [python]1. Objects_in_Python (0) 2022.01.21