3

My package is far more complex, but here's everything stripped out and made easier to read as an example:

sound/                         
      __init__.py               
      formats/                 
              __init__.py
              wavread.py
              wavwrite.py
      effects/                  
              __init__.py
              echo.py

(Note: I borrowed this structure from the official tutorial)

I want to be able to reference echo.py from wavwrite.py.

In wavwrite.py, I made my reference this way:

from ..effects import echo

And I get this error:

File "C:\sound\formats\wavwrite.py", line 1, in <module>
   from ..effects import echo
SystemError: Parent module '' not loaded, cannot perform relative import

I had a friend comment that I should try an absolute import, and in that case, I get a different issue.

In wavwrite.py, I made my reference this way:

from effects import echo

Or

import effects.echo as echo

And I get this error:

ImportError: No module named 'effects'

This all looks correct to me based upon how I understand this should work, and it's driving me nuts. It has to be something simple that I'm missing.

vaultah
  • 44,105
  • 12
  • 114
  • 143
raiderrobert
  • 653
  • 5
  • 16

1 Answers1

1

So I found an answer elsewhere on SO, but I don't like it:

from sys import path
from os.path import dirname as dir
path.append(dir(path[0]))

from effects import echo

This functions, but it looks very ugly, and it seems like it should be unnecessary. Can anyone improve upon this?

Community
  • 1
  • 1
raiderrobert
  • 653
  • 5
  • 16