I have a package installed in my site-packages dir. Folder structure looks like this

MyPkg\
  __init__.py

  LogUtils\
    __init__.py
    logwrapper.py

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

When I do help ('modules') I see MyPkg listed. But I get the following error in 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

Why would LogUtils import fine standing alone, but throw an error when being imported via an init file??

有帮助吗?

解决方案 2

As you see yourself, you are not importing the same module:

>>> from MyPkg import LogUtils

vs.

from LogUtils import logger, log

The first imports a package called MyPkg.LogUtils, the second imports a package called LogUtils. Whether they exist or not depends on your python paths, but in general, if the first one works, change the second one to

from MyPkg.LogUtils import logger, log

其他提示

Seems to me you are lacking some backslashes

MyPkg\
  __init__.py

  LogUtils\
    __init__.py, \
    logwrapper.py

  Shortcuts\
    __init__.py, \
    somefile.py
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top