Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Attempted relative import in non-package not for tests package

Tags:

python

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.

like image 647
codec Avatar asked May 12 '16 17:05

codec


1 Answers

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.

like image 133
wholevinski Avatar answered Nov 02 '22 12:11

wholevinski