I Have following directory structure:
src
__init__.py
foo1
__init__.py
foo1.py
foo2
__init__.py
foo2.py
Now I want to import from foo2.py the module foo1.py. In foo2.py I’ve imported with from ..foo1.foo1 import * but all I get is:
Traceback (most recent call last):
File "foo2.py", line 3, in <module>
from ..foo1.foo1 import *
ValueError: Attempted relative import in non-package
How do I get this relative import?
What I see here is that the base package, in your case
srchas not been imported, this is a necessary condition to use relative imports.If you write an script that the
srcfolder is in thesys.path, thus importable, which states:It will work.