Pergunta

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??

Foi útil?

Solução 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

Outras dicas

Seems to me you are lacking some backslashes

MyPkg\
  __init__.py

  LogUtils\
    __init__.py, \
    logwrapper.py

  Shortcuts\
    __init__.py, \
    somefile.py
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top