I can't understand why the following runs (Python 2.7) when one of the modules isn't imported as far as I understand
# \sound\formats\script.py
import formats.wavread
print formats.wavread.foo()
print formats.wavwrite.boo()
My directory structure is
sound\
__init__.py
formats\
__init__.py
script.py
wavread.py
wavwrite.py
Both __init__.py are empty. The rest have the following code
# \sound\formats\wavread.py
import wavwrite # <-- unused import
def foo():
return "read foo"
# \sound\formats\wavwrite.py
def boo():
return "write boo"
script.py runs fine despite the fact that module wavwrite.py is not imported in my understanding. I guess however that somehow it was imported when import formats.wavread statement was executed because there is an import wavwrite line in the wavread.py module. I was under the impression that this import, import wavwrite, was totally useless, nevertheless it would make a binding to the wavread module's global namespace. Therefore the wavwrite.boo() method would be out of scope and inaccessible from inside script.py. Apparently it doesn't work like that.
Removing the subpackage prefix from the code of the script.py module looks to make the program work as I would expect. Hence if you run the following
#\sound\formats\script.py
import wavread
print wavread.foo()
print wavwrite.boo()
will execute the wavread.foo() method by printing out read foo and will hit an error at the next line where it drops a message NameError: name 'wavwrite' is not defined because as expected the wavwrite module has not been imported.
What exactly is happening here and how import formats.wavread is different to import wavread please?