0

I'm getting this error when running main.py

ValueError: attempted relative import beyond top-level package

I'm expecting this functionality...

  • package
    • __init__.py
    • main.py
    • subpackage1
      • __init__.py
      • sub1_file
    • subpackage2
      • __init__.py
      • sub2_file

main.py

from .subpackage1.sub1_file import fun1
from .subpackage2.sub2_file import fun2

sub1_file

from ..subpackage2.sub2_file import fun2
Phillip Sloan
  • 125
  • 1
  • 2
  • 9
  • You need to run `python3 -m package.main` (with `package` being under an entry in `sys.path`, not `python3 -m main` or `python3 path/to/main.py`. With the former, it knows it's part of a package, with the other two, it thinks it's a standalone module and is confused when you tell it it's part of a package that hasn't even been imported. – ShadowRanger Dec 10 '20 at 04:54
  • Why would it think it's a standalone module? Why would it not assume that the program being run is at the root? When I run main.py, it throws an error saying that the .py files in the submodules are beyond the top level. Also, I'm getting "Relative module names not supported" with `python -m main.py`. – Phillip Sloan Dec 10 '20 at 05:12
  • Yeah. Because relative imports require you to be part of a logical package. `main.py` is logically the module `main` when you run it like that. It's not `package.main`, so it doesn't *have* sibling packages/modules. The package system for Python is similar to the file system, but it's not the same thing; `.` doesn't mean "current directory" it means "current package", and you don't have one (Python has no idea the directory you happen to be in is a package, because you didn't import it via that package name). – ShadowRanger Dec 10 '20 at 05:23
  • So basically folders inside of package aren't seen as subpackages belonging to package? They are seen as their own packages, completely independent of package? – Phillip Sloan Dec 10 '20 at 05:25
  • Python doesn't realize `package` (the folder) is a package in the first place. If a module was imported rooted in `package` (the folder/package), then that module would know it's a package (so running `package.main` the module provides the context to know that `main` is part of package, and can do relative imports within it), but if not, it has no more meaning than the fact that a script named `spam.py` is run from within `/home/phillip`; `spam.py` is a script, and `/home/phillip` is where it is stored, but it doesn't assume it's *really* `home.phillip.spam` (you wouldn't want it to, right?). – ShadowRanger Dec 10 '20 at 05:27
  • I'm getting `blah\python.exe: Relative module names not supported` when trying `python -m package.main`. I still am not fully understanding why python can't understand that the file being run is not the relative root. – Phillip Sloan Dec 10 '20 at 05:35
  • Perhaps, I just do not understand how packages work? I am expecting it to work like basically any other language. As in, here is the path to the file... Load it. Grab the functions. Use them. – Phillip Sloan Dec 10 '20 at 05:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225758/discussion-between-shadowranger-and-phillip-sloan). – ShadowRanger Dec 10 '20 at 05:42

0 Answers0