Bugs item #1453145, was opened at 2006-03-18 14:02 Message generated for change (Comment added) made by loewis You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1453145&group_id=5470
Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: Python Interpreter Core Group: Python 2.4 >Status: Closed >Resolution: Invalid Priority: 5 Submitted By: Ali Gholami Rudi (aligrudi) Assigned to: Nobody/Anonymous (nobody) Summary: Unexpected module reloading Initial Comment: I'll demonstrate the problem: Consider the following package hierarchy: p/ __all__.py m1.py m2.py The contents of m1 and m2 modules are: -----m1.py---- import m2 import p.m2 -------------- -----m2.py---- print 'Loading m2 module' -------------- Running the m1 module would yield the output Loading m2 module Loading m2 module . As it is obvious from the output the module m2 is loaded twice. The problem arrises when you want to do things such as implementing a singleton class: -----Alternate m2.py----- class Singleton(object): _instance = None @staticmethod def getInstance(): if Singleton._instance is None: Singleton._instance = Singleton() return _instace ------------------------- -----Alternate m1.py----- import m2 import p.m2 print m2.Singleton.getInstance() print p.m2.Singleton.getInstance() ------------------------- If you run m1 module, the output shows that the two instaces are not the same objects. That is m2.Singleton and p.m2.Singleton are not the same classes. I think this is a bug. ---------------------------------------------------------------------- >Comment By: Martin v. Löwis (loewis) Date: 2006-03-19 11:44 Message: Logged In: YES user_id=21627 At first, I could reproduce the problem; look at this transcript to see what I did. [EMAIL PROTECTED]:~/tmp$ mkdir p [EMAIL PROTECTED]:~/tmp$ echo >p/__init__.py [EMAIL PROTECTED]:~/tmp$ cat >p/m1.py import m2 import p.m2 [EMAIL PROTECTED]:~/tmp$ cat >p/m2.py print 'Loading m2 module' [EMAIL PROTECTED]:~/tmp$ python Python 2.3.5 (#2, Mar 6 2006, 10:12:24) [GCC 4.0.3 20060304 (prerelease) (Debian 4.0.2-10)] on linux2 Type "help", "copyright", "credits" or "license" for more information. py> import p.m1 Loading m2 module py> As you can see, the "Loading" output is printed only once. This might happen if you run p/m1.py as the main program, but I cannot reproduce it: [EMAIL PROTECTED]:~/tmp$ python p/m1.py Loading m2 module Traceback (most recent call last): File "p/m1.py", line 2, in ? import p.m2 ImportError: No module named p.m2 As the current directory is not on sys.path, it won't find the package p. Now, if you also change that (e.g. by setting PYTHONPATH), I get [EMAIL PROTECTED]:~/tmp$ export PYTHONPATH=`pwd` [EMAIL PROTECTED]:~/tmp$ python p/m1.py Loading m2 module Loading m2 module This is not a bug: Now *both* the current directory, and the directory ~/tmp/p are on sys.path (print sys.path inside m1 to see what I mean). When you do "import m2", it searches for a module named m2 on sys.path, and it finds p/m2.py. When you then import p.m2, it searches for a package p on sys.path, finds the package, and then imports the module m2. It happens that both modules have the same source file - yet they are different modules. If you change the print statement to print 'Loading', __name__, 'module' you get Loading m2 module Loading p.m2 module So in short: this is not a bug. Don't try running a module in a package as the main program. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1453145&group_id=5470 _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com