Issue1453145
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2006-03-18 13:02 by aligrudi, last changed 2022-04-11 14:56 by admin. This issue is now closed.
Messages (2) | |||
---|---|---|---|
msg27822 - (view) | Author: Ali Gholami Rudi (aligrudi) | Date: 2006-03-18 13:02 | |
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. |
|||
msg27823 - (view) | Author: Martin v. Löwis (loewis) * | Date: 2006-03-19 10:44 | |
Logged In: YES user_id=21627 At first, I could reproduce the problem; look at this transcript to see what I did. martin@mira:~/tmp$ mkdir p martin@mira:~/tmp$ echo >p/__init__.py martin@mira:~/tmp$ cat >p/m1.py import m2 import p.m2 martin@mira:~/tmp$ cat >p/m2.py print 'Loading m2 module' martin@mira:~/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: martin@mira:~/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 martin@mira:~/tmp$ export PYTHONPATH=`pwd` martin@mira:~/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. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:56:16 | admin | set | github: 43050 |
2006-03-18 13:02:46 | aligrudi | create |