2014-09-01 48 views
1

我有以下文件夾結構;從源導入結果在處理絕對導入時未找到父模塊

app/ 
ext/ 
    gredis/ 
    gredis.py 

我gredis.py的/Users/blah/blah/blah/ext/gredis/gredis.py

但是完整路徑,當我嘗試使用導入ext.gredis.gredis模塊;

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包含:

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

這個工作在兩個蟒蛇2.x和3.x的