으로 부모 모듈을 돌려처럼 내 자신의 모듈에 대한 더 유용 할 수 있습니다 __package__ 내 컴퓨터에 http://docs.python.org/library/inspect.html#inspect.getmodule
import inspect
parent_module = inspect.getmodule(somefunction)
children = dir(parent_module)
package = parent_module.__package__
당신은 일반적으로 sys.modules
사전에서 그것을 얻을 수 있습니다. 파이썬은 특히 관계가 일대일 관계가 아니기 때문에 이름에 "부모 포인터"를 유지하지 않습니다. 예를 들어, 예를 사용 : 당신이 subprocess
모듈 types
의 존재를 알게 될 것이다 경우
>>> from subprocess import types
>>> types
<module 'types' from '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/types.pyc'>
>>> import sys
>>> sys.modules['subprocess']
<module 'subprocess' from '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.pyc'>
것은에서 import types
문 단지 유물이다. 해당 모듈이 필요한 경우 import types
입니다.
실제로 subprocess
의 이후 버전은 types
을 더 이상 가져 오지 않아 코드가 중단됩니다. 모듈의 __all__
목록에 나타나는 이름 만 가져와야합니다. 다른 이름을 구현 세부 사항으로 간주하십시오.
따라서, 예를 들어 :
>>> import subprocess
>>> dir(subprocess)
['CalledProcessError', 'MAXFD', 'PIPE', 'Popen', 'STDOUT', '_PIPE_BUF', '__all__', '__builtins__', '__doc__',
'__file__', '__name__', '__package__', '_active', '_cleanup', '_demo_posix', '_demo_windows', '_eintr_retry_call',
'_has_poll', 'call', 'check_call', 'check_output', 'errno', 'fcntl', 'gc', 'list2cmdline', 'mswindows', 'os',
'pickle', 'select', 'signal', 'sys', 'traceback', 'types']
>>> subprocess.__all__
['Popen', 'PIPE', 'STDOUT', 'call', 'check_call', 'check_output', 'CalledProcessError']
당신은 subprocess
에 표시 이름의 대부분은 수입 단지 다른 최고 수준의 모듈이라는 것을 확인할 수 있습니다.
예가 이상합니다.'types'는 실제로 표준 파이썬'types' 모듈입니다. import 라인은'import types'와 똑같이 할 것이고,'types'는 두 경우 모두 정확히 같은 모듈 객체가 될 것입니다. –
서브 프로세스는 어떤 의미에서든 의미가 있거나 유용하다고 여겨지는'types' 모듈의 "부모"가 아닙니다. 'subprocess'는 분명히 다른 타입의 import 모듈과 마찬가지로'types'을 가져 왔습니다. 너 뭐하려고? 무엇을하려하십니까? –
그 유형이 표준 모듈이라는 것을 알고 있지만 모든 서브 모듈을 검색 할 때 서브 프로세스 모듈에서 그 유형을 발견했습니다. – jcubic