[ python-Bugs-977250 ] Double __init__.py executing
SourceForge.net
noreply at sourceforge.net
Sat Jan 29 23:02:22 CET 2005
Bugs item #977250, was opened at 2004-06-22 07:55
Message generated for change (Comment added) made by pmoore
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=977250&group_id=5470
Category: Parser/Compiler
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Alexandre (kuskakus)
Assigned to: Nobody/Anonymous (nobody)
Summary: Double __init__.py executing
Initial Comment:
There is some strange feature, looking like a bug.
I have 'pkg' dir with 2 files:
./pkg/__init__.py
print '__init__.py'
./pkg/test.py
print 'test.py'
import __init__
Python 2.3.4 (#53, May 25 2004, 21:17:02)
>>> import pkg.test
__init__.py
test.py
__init__.py
With '-v' option:
>>> import pkg.test
import pkg # directory pkg
# pkg\__init__.pyc matches pkg\__init__.py
import pkg # precompiled from pkg\__init__.pyc
__init__.py
# pkg\test.pyc matches pkg\test.py
import pkg.test # precompiled from pkg\test.pyc
test.py
# pkg\__init__.pyc matches pkg\__init__.py
import pkg.__init__ # precompiled from pkg\__init__.pyc
__init__.py
Why __init__.py executed two times?
----------------------------------------------------------------------
Comment By: Paul Moore (pmoore)
Date: 2005-01-29 22:02
Message:
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.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=977250&group_id=5470
More information about the Python-bugs-list
mailing list