Suppose there are two files a.py
and b.py
. Inside them are the following:
# a.py
from b import foo
foo()
# b.py
import os
def foo():
print(os.cwd())
This works just fine, but why is a.py
unable to see the imports of b.py
implicitly for its own use:
# a.py
import b
def bar():
print(os.cpu_count()) # fails
b.foo()
bar()
# b.py
import os
def foo():
print(os.cwd())
I am reading the import system docs but haven't been able to figure this out. The os
import exists in the namespace of b.py
but it's not available to a.py
and I am unsure why. The globals()
method is at the module level, so given the rules, is there no sharing of imports between modules (without using __all__
and *
imports)? Does each module get its own import namespace?