简体   繁体   中英

Attempted relative imports beyond top-level package?

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM