The header might be quite hard to understand, but I'll try to explain as well as I can.
I have a folder called SubModule which includes total of 3 files:
__init__.py Class1.py Class2.py Inside __init__.py, I got this:
import Class1 import Class2 def main(): c1 = Class1.Class1() c2 = Class2.Class2() if __name__ == "__main__": main() And it all works fine with no errors, and I can use c1 and c2 properly.
The problem appears, when I try to move SubModule folder under an other module's (let's call it MainModule) folder. So I would have the following:
MainModule\__init__.py MainModule\SubModule\__init__.py MainModule\SubModule\Class1.py MainModule\SubModule\Class2.py Now when I try to import SubModule from MainModule\__init__.py like so:
import SubModule c1 = SubModule.ClassOne.ClassOne() c2 = SubModule.ClassTwo.ClassTwo() I would expect this to work. However, running the MainModule\__init__.py raises an error from SubModule\__init__.py:
Traceback (most recent call last): File "C:\...\MainModule\__init__.py", line 1, in <module> import SubModule File "C:\...\MainModule\SubModule\__init__.py", line 1, in <module> import Class1 ImportError: No module named 'Class1' As you can see, the error comes from import Class1, and it tells me that there's No module named 'Class1'. This is cause the path is now SubModule.Class1, instead of only Class1. I can get rid of this error by changing SubModule\__init__.py to this:
import SubModule.Class1 import SubModule.Class2 However, I can no longer use the SubModule alone by running SubModule\__init__.py, since the path would be import Class1 again, instead of import SubModule.Class1.
This is making me crazy, is there a way to generalize the importing, so it doesn't matter which module imports SubModule?