I have searched related questions and tested many solutions there, for example, I found [a solution here]:https://askubuntu.com/a/470989/608783, but I found it does not work in my case...
I have the following file structure:
Folder1
Folder2
__init__.py
model.py
Folder3
__init__.py
test.py
In model.py
I defined a class:class mymodel()
, then in test.py
I wanna to use mymodel
, so I tried from ..Folder2 import mymodel
, from ..Folder2.model import mymodel
, but all failed with error ValueError: Attempted relative import in non-package
.
What should I do? Thanks for any advice.
Edit: I am very sorry for not list out _init_.py
files, they are indeed included in the Folder2 and Folder3. However, the content of them are different from which proposed by @anekix's solution.
The following are from my possibly wrong setting:
__init__.py
in Folder2:from .model import mymodel
__init__.py
in Folder3: empty
What is wrong with my two __init__.py
, thank you again.
Update: One way to solve this problem is by set all __init__.py
to empty and then follow @jeffy proposed solution. Another way is by @anekix proposed solution, which should also works for this kind of problem(you should have a try) although not in my case, possibly because the whole project of mine is too messy.