1

I'm having trouble with relative imports, but I think its because i'm not understanding package structure completely.

For example, here is my package structure.

neo_autorig/                          Source folder, Top level
      __init__.py               
      basic/                  Subpackage for basic utiltites for the script
              __init__.py
              name.py

      name_test.py
      module_locator.py

Theres more than this, but this is basically what i'm using for imports

In name.py i'm importing module locator using

from .. import module_locator

But it says

# Error: line 1: Attempted relative import beyond toplevel package

Are top level scripts (like my main script/ui used to execute everything) supposed to go in the top source folder in the eclipse package? Or am i setting this up wrong. There are also other sub packages in the source folder, each with scripts in them.

Edit: If i putanother package in a sub package, I can relative import, its only the case where i cant relative import from a sub package to a top level package, and the scripts source is in my python path.

Neo Conker
  • 169
  • 1
  • 13

1 Answers1

1

The python import mechanism works with the __name__ of the file. Executing a file directly gives the file the name "__main__" instead of its usual name. The common answer to questions like this would be to run the program with the -m option. I recommend reading pep 366 and maybe this or this question as well.

Community
  • 1
  • 1
Muttonchop
  • 353
  • 4
  • 22