2014-03-13 52 views
1

我在我的站點包目錄中安裝了一個軟件包。文件夾結構看起來像這樣從__init__文件導入時出現python錯誤

MyPkg\ 
    __init__.py 

    LogUtils\ 
    __init__.py 
    logwrapper.py 

    Shortcuts\ 
    __init__.py <-----this references LogUtils 
    somefile.py 

當我做help ('modules')我看到列出MyPkg。但我在IDLE中出現以下錯誤:

>>> import MyPkg 
>>> from MyPkg import LogUtils 
>>> from MyPkg import Shortcuts 

Traceback (most recent call last): 
    File "<pyshell#2>", line 1, in <module> 
    from MyPkg import Shortcuts 
    File "C:\Python27\lib\site-packages\MyPkg\Shortcuts\__init__.py", line 1, in <module> 
    from GoToUrl import go_to_url 
    File "C:\Python27\lib\site-packages\MyPkg\Shortcuts\GoToUrl.py", line 1, in <module> 
    from LogUtils import logger, log 
ImportError: No module named LogUtils 

爲什麼LogUtils可以單獨導入,但是在通過init文件導入時會拋出錯誤?

回答

0

正如你看到自己,你是不是導入相同的模塊:

>>> from MyPkg import LogUtils 

from LogUtils import logger, log 

第一導入一個名爲MyPkg.LogUtils的程序包,第二個程序包導入一個名爲LogUtils的程序包。它們是否存在取決於你的python路徑,但是一般情況下,如果第一個工作,將第二個改爲

from MyPkg.LogUtils import logger, log 
0

我看來,你所缺乏的一些反斜槓

MyPkg\ 
    __init__.py 

    LogUtils\ 
    __init__.py, \ 
    logwrapper.py 

    Shortcuts\ 
    __init__.py, \ 
    somefile.py