15
votes

I know this has been asked many times but somehow I am not able to get over this error. Here is my directory structure-

project/
  pkg/
  __init__.py
  subpackage1/
        script1.py
        __init__.py
  subpackage2/
        script2.py
       __init__.py

script2.py has:

class myclass:
    def myfunction:

script1.py has

 from ..subpackage2 import script2   

I also tried

from ..subpackage2 import myclass

And this gives me : ValueError: Attempted relative import in non-package

Any help would be really appreciated.

1

1 Answers

18
votes

This answer explains what's going on: https://stackoverflow.com/a/73149/769971

You're probably running script1.py from inside the subpackage1/ directory. Change your import to be from subpackage2 import script2, back up to the pkg/ directory, then run python -m subpackage1.script1.