Package organization: where to put 'common' modules?

fortepianissimo fortepianissimo at gmail.com
Mon Mar 6 18:55:13 CET 2006


Kent Johnson wrote:
> Paul Boddie wrote:
> > Yes, Python does this - it puts the directory of bar.py (B in this
> > case) in sys.path, but not the directory in which you're sitting when
> > you run the program from the shell (A in this case).
>
> This seems to be OS dependent. If I put 'print sys.path' at the start of
> site.py (which does most of the setup of sys.path), one element of the
> path is an empty string. This is expanded by
> main() -> removeduppaths() -> makepath() -> os.path.abspath()
> to the current working dir.
>
> This is Python 2.4.2 on Win2K.
>
> Kent

Following your example, I tried to use symlink to solve my problem. I
have the following dir structure:

A
|--- util
|    |--- foo.py
|
|--- B
|    |--- util (symlink to ../util)
|    |--- bar.py
|
|--- main.py

------
util/foo.py:

print 'foo initialized'

def baz():
     print 'foo.baz() here'

------
B/bar.py:

from util import foo
foo.baz()

------
main.py:

import sys

from util import foo
print id(sys.modules['util.foo'])

from B import bar
print id(sys.modules['util.foo'])
------

Now when I run

python main.py

in dir A, I got the following result (Python 2.4.2, Mac OS X):

foo initialized
3698320
foo initialized
foo.baz() here
3698320


My question is why foo got initialized twice?




More information about the Python-list mailing list