image.png

def echo_test():
    print('에코')

new file 눌러서 render.py 만듦

def render_test():
    print('랜더링')
import echo
import render

echo.echo_test()
render.render_test()

->에코
  랜더랑
# 테스트1 명시적이다. 그렇지만 길다
# 패키지를 만들때는 폴더를 하나 만들어야한다
# new file 눌러서 echo.py 만듦
# new file 눌러서 render.py 만듦
# import echo
# import render
# echo.echo_test()
# render.render_test()
# game이라는 폴더를 만들어서 echo.py render.py를 옮김
# 폴더로 옮기고 다시 실행시키면 에러가 뜸
# 위에
# %%cmd
# set PYTHONPATH= D:\\pmj\\python\\game
# 이거 안해줘도 경로 다 적어주면 된다
# 즉 import할 때 경로 설정은 폴더이름.폴더이름.파일이름 이런 식으로 적어주면 된다
import game.sound.echo
import game.graphic.render
game.sound.echo.echo_test()
game.graphic.render.render_test()
# echo.py를 하나 더 만듦 충돌을 생각해서..
->에코
  랜더링
def echo_test():
    print('비디오에코')
# graphic 안에 뒀던 echo.py를 echo_v.py로 바꾸고 다 game 폴더로 다시 옮김
# import game.echo, game.echo_v
# from game.echo import echo_test
# from game.echo_v import echo_test 
# echo_test()
# echo_test()
# 중복된 내용이 있다면 마지막의 것만 실행된다
from game.echo import echo_test as e
from game.echo_v import echo_test as ev
e()
ev()
# 이렇게 별칭을 쓰면 해결되긴 하는데 별칭도 충돌이 생길 수 있다

->에코
비디오에코
# import game.graphic.echo # graphic 내부에 있는 echo_test
# import game.sound.echo # sound 내부에 있는 echo_test
# 이렇게 변경하면 됨
# from game.graphic.echo import echo_test
# from game.sound.echo import echo_test
# 두 줄을 이렇게 한줄로 변경가능 함
from game import graphic.echo, sound.echo
graphic.echo_test()

->__init__.py 를 사용하지 않아서 에러
def print_version_info():
    print(f'The version of this game is {VERSION}')

game/sound의 init.py

VERSION = 3.5

def print_version_info():
    print(f'The version of this sound of game is {VERSION}')

game/graphic의 init.py

VERSION = 3.7

def print_version_info():
    print(f'The version of this graphic of game is {VERSION}')
# game, sound, graphic 폴더에 가서 __init__.py파일을 만들어줌
# __init__.py 를 만들어주기 전까지는 파일 경로를 길에 나열해야했지만 __init__.py에 미리 적어두면 간단하게 game만 import 시켜도 실행가능하다
import game
print(game.VERSION)
# print(game.print_version_info()) # -> none이 나오기 때문에 함수만 실행해보기
game.print_version_info()

->3.6
The version of this game is 3.6