2011-05-05 4 views
6

을 가져 오기 전에 패키지의 위치를 ​​가져 오려고합니다. 기본적으로하고 싶습니다가져 오기 전에 파이썬 패키지 위치 결정

import pkg 
pkg_path = pkg.__file__ 

import pkg없이.

target = "pkg" 
target_path = None 
for p in sys.path: 
    search_path = "%s/%s" % (p, target) 
    if os.path.exists(search_path): 
     target_path = search_path 

을하지만이 작동하지 않습니다 몇 가지 시나리오는 (targettarget 압축 된 EGG 파일 안에, __init__.py를 포함하지 않음)이 있습니다 : 지금은하고 있어요.

더 좋은 방법이 있습니까 target_path?

감사합니다,

이안

답변

5

예, imp.find_module()가 :

target_path = __import__('pkg').__file__ 

__import__()import 하나의에 의해 사용됩니다

target_path = imp.find_module(target) 
0

당신은 다음과 [__import__()][1]을 사용할 수 있습니다 그 용도는 e 모듈 이름은 런타임에만 알려집니다.