I have a trouble with imports in my project.
My directories structure is the following:
base_directory
- examples
- src
- folder_1
- __init__.py
- file.py
- folder_2
- __init__.py
- class1.py
- class2.py
- class3.py
- class4.py
In file.py
I'm trying: from ..folder2.class1 import Class1
then, I get the error:
ImportError: attempted relative import with no known parent package
In folder2/__init__.py
I did what I saw on a tutorial for making packages in Python:
from class1 import Class1
my_class_1 = Class1()
So far, anything has worked. What should I do? I use Python 3.7.5
Thanks.
Best Answer
In your example, folder_1 and folder_2 are two separate and unique packages. There are no relative imports between them. Put them in a single outer package to get it to work
Running test.py script
But there is a workaround where modules can be called using the "-m" option. But his only works if
myproject
is in the python path. It works here because I'm in the parent of myproject when I call it.