I understand what is wrong?
Yes, since the line from file_a import A only imports the class A into the file_b namespace. The file_a namespace remains valid. If this were not so, there would be little point in having both syntaxes:
import modulename from modulename import something
as if your thinking was correct, then after the second form you can always use modulename.someotherthing .
If so, is there a way to have common imports and global variables in files?
Yes, with a star * :
from modulename import *
but this leads to a problem of namespace pollution, for example from file_a import * imports into file_b all imports made in file_a . Ultimately, you lose control of your imports and it will bite you at some time ... trust me, what it really is!
If for some reason you need from module import * , a workaround to namespace pollution is to define the __all__ variable in module , which indicates what should be imported using the star operator.
NTN!
source share