2014-09-01 4 views
1

다음 폴더 구조가 있습니다. 내가 사용하여 ext.gredis.gredis 모듈을 가져올 때 절대 가져 오기를 처리하는 동안 부모 모듈의 소스 결과가 없습니다.

app/ 
ext/ 
    gredis/ 
    gredis.py 

나는, /Users/blah/blah/blah/ext/gredis/gredis.py 그러나

에서 gredis.py의 전체 경로를 가지고;

imp.load_source('ext.gredis.gredis', path) 

다음과 같은 오류가 있습니다.

RuntimeWarning: Parent module 'ext.gredis' not found while handling absolute import 

먼저 ext.gredis를 가져와야합니까?

참고 : 모든 폴더가 __init__.py

답변

2

이 오류를 재현 한 코드를 게시 할 수 있나요? 이것은 나를 위해 작동 :

$ tree 
. 
+-- app 
¦   +-- test.py 
+-- ext 
¦   +-- gredis 
¦   ¦   +-- gredis.py 
¦   ¦   +-- gredis.pyc 
¦   +-- test.py 
+-- test.py 

$ for path in test.py app/test.py ext/test.py; do python $path; done; 
<module 'ext.gredis.gredis' from '/tmp/bla/ext/gredis/gredis.pyc'> 
<module 'ext.gredis.gredis' from '/tmp/bla/ext/gredis/gredis.pyc'> 
<module 'ext.gredis.gredis' from '/tmp/bla/ext/gredis/gredis.pyc'> 

그리고 test.py는 포함이 파이썬 2.x 및 3.x에서 모두 작동

import imp 
print(imp.load_source('ext.gredis.gredis', '/tmp/bla/ext/gredis/gredis.py')) 

관련 문제