Logged In: YES
user_id=113328
Essentially, because pkg\__init__.py is loaded into
sys.modules under *two* names. First, as pkg (as part of the
process of importing pkg.test) and then as pkg.__init__
(explicitly in pkg\test.py). As the source is loaded twice,
the print is executed twice.
While subtle, this is not a bug.
To demonstrate:
>>> import sys
>>> orig = set(sys.modules.keys())
>>> import pkg.test
init
init
test
>>> set(sys.modules.keys()) - orig
set(['pkg.test', 'pkg', 'pkg.__init__'])
You see, both pkg and pkg.__init__ are separately in
sys.modules (even though they refer to the same file).
In essence, it is incorrect to import __init__ directly -
always import the package. If test.py had said "import pkg",
you wouldn't have seen this behaviour, but nothing else
would have changed.
|