1

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 Answers 2

1

Seems to me you are lacking some backslashes

MyPkg\
  __init__.py

  LogUtils\
    __init__.py, \
    logwrapper.py

  Shortcuts\
    __init__.py, \
    somefile.py
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.