Relative Import: No Known Parent Package

25 Views Asked by At

I'm trying to do a relative import. From my understanding, I should have an __init__.py file to suggest this is a module.

Let's say I have:

$ ls
__init__.py foo.py bar.py

__init__.py is an empty file. Additionally:

$ cat foo.py
from .bar import MyExampleClass

thing = MyExampleClass()

and

$ cat bar.py
class MyExampleClass():
    pass

So, why do I get?

ImportError: attempted relative import with no known parent package
1

There are 1 best solutions below

2
Vinay Davda On

Your directory which contains all this file will be considered as a package and now you can update code in foo.py:

from .bar import MyExampleClass

to

from bar import MyExampleClass

Other thing should work as expected