I have a strange problem regarding import classes from subfolders.
I'm using Python 3.6
, therefore an __init__.py
should be not required in the subfolders.
I have the following file structure:
root
├── script.py (main)
└── custom
├── class1.py
└── class2.py
This is script.py
:
from custom.class1 import Class1
from custom.class2 import Class2
if __name__ == '__main__':
cl1 = Class1()
cl2 = Class2()
This is class1.py
:
class Class1():
def __init__(self):
print('Class1')
if __name__ == '__main__':
cl1 = Class1()
This is class2.py
, which imports also class1:
from class1 import Class1
class Class2():
def __init__(self):
cl1 = Class1()
print('Class2')
if __name__ == '__main__':
cl2 = Class2()
And now the problem:
It works without error, when i am running python class1.py
in the custom
subfolder.
It works without error, when i am running python class2.py
in the custom
subfolder.
But when i am running python script.py
in the root
folder, i get the following error:
Traceback (most recent call last):
File .... in <module>
from custom.class2 import Class2
File .... line 1, in <module>
from class1 import Class1
ModuleNotFoundError: No module named 'class1'
How can this be fixed in a way, that the scripts in the custom
subfolders can be run on its own and also the script in the root
folder works?