I have the following folder structure:
├── aaa
│ ├── __init__.py
│ ├── ttt
│ │ ├── aaa.py
│ │ └── __init__.py
│ └── ttt.py
├── __init__.py
├── t.py
└── ttt.py
The main script t.py:
import sys
sys.path = ['.']
import ttt
import aaa.ttt
import aaa.ttt.aaa
print(ttt.get_name())
print(aaa.ttt.get_name2())
print(aaa.ttt.aaa.get_name3())
The imported scripts:
./ttt.py
def get_name():
return "ttt"
./aaa/ttt.py
def get_name2():
return "aaa/ttt"
./aaa/ttt/aaa/ttt.py
def get_name3():
return "aaa/ttt/aaa"
Now running the main script:
$ python -B t.py
ttt
Traceback (most recent call last):
File "t.py", line 10, in <module>
print(aaa.ttt.get_name2())
AttributeError: module 'aaa.ttt' has no attribute 'get_name2'
What is the problem here? I am using Python 3.6.1.
Edit
As commented by @AChampion, a problem is that I have
aaa/ttt.py
and aaa/ttt/__init__.py
. However, if I remove the latter I get a ModuleNotFoundError
on import aaa.ttt.aaa
:
$ python -B t.py
Traceback (most recent call last):
File "t.py", line 7, in <module>
import aaa.ttt.aaa
ModuleNotFoundError: No module named 'aaa.ttt.aaa'; 'aaa.ttt' is not a package
Does this mean that if you have a package aaa.bbb
in Python, then you cannot have any modules in package aaa
named bbb
? This smells bad design, so I guess I must be missing something here?