All the Python docs I’ve read appear to indicate that, side-effects aside, that if you import module A and then reference A.a, you are referencing the same variable as if you wrote “from A import a”.
However, that doesn’t appear to be the case here and I’m not sure what’s going on. I’m using Python 2.6.1.
If I create a module alpha.py:
bravo = None
def set_bravo():
global bravo
bravo = 1
Then create a script that imports the module:
import sys, os
sys.path.append(os.path.abspath('.'))
import alpha
from alpha import bravo
alpha.set_bravo()
print "Value of bravo is: %s" % bravo
print "Value of alpha.bravo is: %s" % alpha.bravo
Then I get this output:
Value of bravo is: None
Value of alpha.bravo is: 1
Why is that?
from ... import ...always binds immediately, even if a previousimportonly imported the module/package.EDIT:
Contrast the following: