1

I have a directory structure like the following:

game/
    graphics/
          __init__.py
          render.py
    sound/
         __init__.py
         echo.py
    __init__.py

and my render.py reads from ..sound.echo import echo_test, but every time I run it it says Attempted relative imports beyond top-level package.
How can I fix this?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
roydejung
  • 11
  • 1
  • 2
  • 4
    Have you seen this? https://stackoverflow.com/questions/11536764/how-to-fix-attempted-relative-import-in-non-package-even-with-init-py?rq=1 – OneCricketeer Jan 11 '17 at 03:39
  • @cricket to be candid, i studied python only for 2 days and can get any of the comments in your link..!! i'm using pycharm, and when i typed 'from game.sound.echo', it worked well. i don't get the differene between relative importing and this. – roydejung Jan 11 '17 at 03:57
  • 1
    Well, if you studied for only two days, I might suggest to perhaps start at the tutorial. https://docs.python.org/3/tutorial/ – OneCricketeer Jan 11 '17 at 03:58

2 Answers2

2

Do you need a relative import?

Can you use from game.sound.echo import echo_test?

Though, since you haven't shown the exact stacktrace, the error could be coming from echo.py. An attempted "import beyond top-level package" is self-explanatory, though.

PEP 328 could be worth a read

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

I'm guessing that you are trying the following, or something similar: python render.py

But this is treating render.py as a script and not a component of a package. If you write an external script, where you import render, and then test render functions, the imports should work fine.

You may want to have a look at Hitchhiker's Guide to Python

Dylan
  • 31
  • 3