Python runs the code in __init__.py when the package is imported, which allows for some initialization. However, just because it is executed does not mean that you have access to the variables in it from other modules.
For instance:
testpackage __init__.py testmod.py
Let's say __init__.py has the print "Initializing __init__" code print "Initializing __init__" , and testmod.py has the print "Initializing testmod" . In this case, importing testpackage or testmod will testmod initialization code:
dynamic-oit-vapornet-c-499:test dgrtwo$ python -c "import testpackage" Initializing __init__ dynamic-oit-vapornet-c-499:test dgrtwo$ python -c "from testpackage import testmod" Initializing __init__ Initializing testmod
However, it does not give testmod.py access to the variables from __init__.py . This must be done explicitly.
source share