Reputation:
In file foo.py I want to import class A from file main.py with the following folderStructur:
main.py
__init__.py
|-folder1
__init__.py
|-folder 2
__init__.py
foo.py
|-folder 3
...
How does the import-line has to look like? I tried things like
from ..main import A
but i just got
ValueError: Attempted relative import in non-package
Upvotes: 0
Views: 780
Reputation: 526573
That error comes from how you're running the file - if you want to do that relative import, you need to run your foo program like this:
>>> import topfolder.folder1.folder2.foo
>>> foo.run()
(In other words, you need to be treating the package structure from which you are importing as an actual package.)
Upvotes: 2