https://wikidocs.net/1418
game 이라는 상위디렉터리 하위에 아래처럼 디렉터리와, 파이선 모듈을 생성한다.
__init__.py 파이썬 모듈에 내용은 비어둔채 생성한다.
__init__.py 이란?
이 파일은 해당 디렉터리가, 패키지임을 알려주는 역할이다.
즉, __init__.py 이 없다면, game, sound, graphic 은 패키지로 인식되지 않는다.
[root@tpl-master game]# tree
.
|-- __init__.py
|-- graphic
| |-- __init__.py
| `-- render.py
`-- sound
|-- __init__.py
`-- echo.py
2 directories, 5 files
sound 패키지안에 echo.py 파이썬 모듈과, graphic 패키지안에 render.py 파이썬 모듈을 보자. ( __init__.py 은 비워둔다. )
[root@tpl-master game]# cat sound/echo.py graphic/render.py
def echo_test():
print "echo~"
def render_test():
print "render~"
패키지 참조하기
game.sound 패키지를 참조해,
game 이라는 상위디렉터리 하위에 아래처럼 디렉터리와, 파이선 모듈을 생성한다.
__init__.py 파이썬 모듈에 내용은 비어둔채 생성한다.
__init__.py 이란?
이 파일은 해당 디렉터리가, 패키지임을 알려주는 역할이다.
즉, __init__.py 이 없다면, game, sound, graphic 은 패키지로 인식되지 않는다.
[root@tpl-master game]# tree
.
|-- __init__.py
|-- graphic
| |-- __init__.py
| `-- render.py
`-- sound
|-- __init__.py
`-- echo.py
2 directories, 5 files
sound 패키지안에 echo.py 파이썬 모듈과, graphic 패키지안에 render.py 파이썬 모듈을 보자. ( __init__.py 은 비워둔다. )
[root@tpl-master game]# cat sound/echo.py graphic/render.py
def echo_test():
print "echo~"
def render_test():
print "render~"
패키지 참조하기
game.sound 패키지를 참조해,
echo모듈에 있는 echo_test 라는 함수
를 호출하려면
from game.sound import echo
위처럼, 참조하고, echo.echo_test() 처럼 echo_test() 함수를 호출한다.
[root@tpl-master python]# python
Python 2.6.4 (r264:75706, Oct 23 2012, 16:21:35)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>>>>import game.sound.echo
>>>game.sound.echo.echo_test()
echo~
>>>>>>
[root@tpl-master python]# python
Python 2.6.4 (r264:75706, Oct 23 2012, 16:21:35)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>>>>import game.sound.echo
>>>game.sound.echo.echo_test()
echo~
>>>>>>
from game.sound import echo
>>>echo.echo_test()
echo~
>>>from game.graphic import render
>>>render.render_test()
render~
>>>
__all__ 이란?
특정 디렉터리의 모듈을
echo~
>>>from game.graphic import render
>>>render.render_test()
render~
>>>
__all__ 이란?
특정 디렉터리의 모듈을
*
을 이용해, import 할경우에는 해당 디렉터리의 __init__.py 파일에 __all__ 이라는 변수를 설정하고, import 가능한
모듈을
정의해 주어야 한다.
sound 패키지 안에 있는 echo.py 파이썬 모듈은
sound 패키지 안에 있는 echo.py 파이썬 모듈은
echo
이고,
graphic 패키지 안에 있는 render.py 파이썬 모듈은
graphic 패키지 안에 있는 render.py 파이썬 모듈은
render
이다.
즉
echo __all__ = [\'
즉
echo __all__ = [\'
echo
\']>sound/__init__.py
echo __all__ = [\'
echo __all__ = [\'
render
\']>graphic/__init__.py
처음에 game,sound, graphic 패키지 안에 __init__.py 이 내용이 비어었었기 때문에, from game.sound import * 를 하고, echo 모듈을 사용하려고 하면
처음에 game,sound, graphic 패키지 안에 __init__.py 이 내용이 비어었었기 때문에, from game.sound import * 를 하고, echo 모듈을 사용하려고 하면
echo를 찾을수 없다는 에러가
난다.
당연히 from game.sound.echo import * 라고 참조 하고 echo모듈에 echo_test() 를 호출하면, 정상적으로 처리가 되는데, 이는 from의 마지막항목인 echo가 모듈 이기 때문이다.
[root@tpl-master game]# echo __all__ = [\'echo\']>sound/__init__.py
[root@tpl-master game]# echo __all__ = [\'render\']>graphic/__init__.py
파일안에 모듈이 잘 선언 되어있는지 확인 해보자.
[root@iaas-dms2-mysql-tpl-master game]# cat sound/__init__.py graphic/__init__.py
__all__ = ['
당연히 from game.sound.echo import * 라고 참조 하고 echo모듈에 echo_test() 를 호출하면, 정상적으로 처리가 되는데, 이는 from의 마지막항목인 echo가 모듈 이기 때문이다.
[root@tpl-master game]# echo __all__ = [\'echo\']>sound/__init__.py
[root@tpl-master game]# echo __all__ = [\'render\']>graphic/__init__.py
파일안에 모듈이 잘 선언 되어있는지 확인 해보자.
[root@iaas-dms2-mysql-tpl-master game]# cat sound/__init__.py graphic/__init__.py
__all__ = ['
echo
']
__all__ = ['
__all__ = ['
render
']
자, 그럼 인터프리터에서 확인을..
[root@tpl-master python]# python
Python 2.6.4 (r264:75706, Oct 23 2012, 16:21:35)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>>>>from game.sound import *
>>>echo.echo_test()
echo~
>>>from game.graphic import *
>>>render.render_test()
render~
>>>
render.py 모듈이 sound패키지의 echo.py 모듈을 사용하려고 한다면?
너무 간단하다. sound에 echo모듈을 참조하면 된다.
자, 그럼 인터프리터에서 확인을..
[root@tpl-master python]# python
Python 2.6.4 (r264:75706, Oct 23 2012, 16:21:35)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>>>>from game.sound import *
>>>echo.echo_test()
echo~
>>>from game.graphic import *
>>>render.render_test()
render~
>>>
render.py 모듈이 sound패키지의 echo.py 모듈을 사용하려고 한다면?
너무 간단하다. sound에 echo모듈을 참조하면 된다.
from
game
.sound import *
또는
from
..
sound import *
..
은 부모디렉터리
. 은 현재 디렉터리
참조가 잘되었는지 render_test() 함수에
echo모듈에 echo_test() 함수를 호출
해보자.
[root@pl-master python]# cat game/graphic/render.py
[root@pl-master python]# cat game/graphic/render.py
##from
game
.sound import *
from
..
sound import *
def render_test():
print "render~"
print "render~"
echo.echo_test()
자, 그럼 인터프리터에서
render 모듈에 render_test() 함수를 호출
했을때, echo모듈에 echo_test()에 echo~ 가 출력되는지 확인을 해보자.
[root@tpl-master python]# python
Python 2.6.4 (r264:75706, Oct 23 2012, 16:21:35)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>>>>
[root@tpl-master python]# python
Python 2.6.4 (r264:75706, Oct 23 2012, 16:21:35)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>>>>
from game.graphic import *
>>>
render.render_test()
render~
echo~
>>>
echo~
>>>
'파이썬' 카테고리의 다른 글
인수 전달(sys.argv) , 스크립트 강제 종료 (sys.exit()) (0) | 2015.11.10 |
---|---|
예외처리_파일읽기 예제로 (0) | 2015.11.10 |
모듈만들고 불러오기_2 (sys.path.append(모듈절대경로)) (0) | 2015.11.10 |
파이썬 공부하기_모듈 만들고 불러보기 ( if __name__ == "__main__" ) (0) | 2015.11.10 |
파이썬 공부하기_클래스 (상속, 함수 오버라이딩, 연산자 + 오버라이딩) (0) | 2015.11.10 |