user2329125
user2329125

Reputation:

Importing classes in python 3.2

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

Answers (1)

Amber
Amber

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

Related Questions