네이버 부스트코스 <인공지능 기초다지기> 강의를 참고하여 작성하였습니다.
: 어떤 대상의 부분/조각
: 모듈을 모아놓은 단위, 하나의 프로그램
=> pycache 파일이 생긴다.
: 파이썬을 쉽게 호출할 수 있도록 기계어로 번역해서 파이캐시 파일에 저장한다. 내 폴더를 메모리로 로딩할 때 빠르게 하기위해서 컴파일한 폴더구나~라고 생각하면 된다.
> module_ex.py
import fah_converter
print("Enter a celcius value: ")
celcius = float(input())
fahrenheit = fah_converter.convert_c_to_f(celcius)
print("That is ", fahrenheit, "degrees Fahrenheit")
> fah_converter.py
def convert_c_to_f(celcius_value):
return celcius_value * 9.0 / 5 + 32
출력값:
Enter a celcius value:
36.5
That is 97.7 degrees Fahrenheit
Alias 설정하기 : 모듈명을 별칭으로 사용
모듈에서 특정 함수 / 클래스만 호출하기
모듈에서 모든 함수 / 클래스를 호출하기
#1.
import fah_converter as fah
#2.#convert_c_to_f 함수만 호출
from fah_converter import convert_c_to_f
#3.#전체 호출
from fah_converter import *
import random
print(random.randint(0,100)) # 0~100사이의 정수 난수 생성
print(random.random()) # 일반적인 난수 생성
import time
print(time.localtime()) # 현재 시간 출력
import urllib.request
response = urllib.request.urlopen("http://~")
print(response.read())
: 하나의 대형 프로젝트를 만드는 코드의 묶음
#echo.py
def echo_play(echo_number):
print("echo {} number start".format(echo_number))
#image/__init__.py
__all__ = ["character","object"]
from . import character
from . import object
#stage/__init__.py
__all__ = ["main","sub"]
from . import main
from . import sub
#sound/__init__.py
__all__ = ["bgm","echo"]
from . import bgm
from . import echo
#__init__.py #상위폴더에있는 파일
__all__ = ["image","sound","stage"]
from . import image
from . import sound
from . import stage
요렇게 하위 폴더에 있는 파일들을 모두 import 해준다.
object.py => object_type.py로 Rename
game 폴더안에 main.py 만들어줌
#__main__.py
from sound import echo
if __name__ == '__main__':
print("Game Start")
print(echo.echo_play(3))
=> 프로젝트 내에
🎈 참고>
package namespace: Package 내에서 다른 폴더의 모듈을 부를 때, 상대 참조로 호출하는 방법
#1. 절대 참조
from game.graphic.render import render_test()
#2. . :(점 하나) 현재 디렉토리 기준
from .render import render_test()
#3. .. :(점 둘) 부모 디렉토리 기준
from ..sound.echo import echo_test()
파이썬은 C로 되어있다. 현재는 conda를 더 많이 사용한다.
conda create -n my_project python=3.8
//conda create: 가상환경 새로 만들기
//-n my_project : 가상환경 이름
//python=3.8: 파이썬 버전
conda activate / deactivate가 뜬다!
가상환경 conda를 활성화시키기위해
conda activate my_project
입력하면
가상환경 안으로 들어온다
C:\Users\user>conda install matplotlib
Collecting package metadata (current_repodata.json): done
Solving environment: done
conda install <패키지명>
conda install tqdm
Collecting package metadata (current_repodata.json): done
Solving environment: done
conda install jupyter
작업하고 있는 파일로 들어간 후에 설치했다.
- 아나콘다로 파이썬을 사용하는 경우
conda install matplotlib- 파이썬만 사용하는 경우
pip install matplotlib- 주피터 노트북 셀에 입력하는 경우 (or 코랩에서 설치하는 경우)
!pip install matplotlib
비주얼코드로 matplotlib가 작동하지 않아서 cmd에서 파이썬 파일을 실행하니, 실행되었다.
tqdm 도 실습해보자~!~
#tqdm_ex.py
from tqdm import tqdm
import time
for i in tqdm(range(10000)):
if i % 1000 == 0:
time.sleep(1)