site.py confusion

Arnaud Delobelle arnodel at googlemail.com
Wed Jan 27 12:57:59 EST 2010


George Trojan <george.trojan at noaa.gov> writes:

> Inspired by the 'Default path for files' thread I tried to use
> sitecustomize in my code. What puzzles me is that the site.py's main()
> is not executed. My sitecustomize.py is
> def main():
>     print 'In Main()'
> main()
> and the test program is
> import site
> #site.main()
> print 'Hi'
> The output is
> $ python try.py
> Hi

That's normal as site.py is automatically imported on initialisation.
So when you do:

    import site

the module is not re-executed as it already has been imported.
Try this:

    ---- file: foo.py
    print 'Foo'
    ---- end

    --- Interactive session
    >>> import foo # First time, print statement executed
    Foo
    >>> import foo # Second time, print statement not executed
    >>>

> When I uncomment the site.main() line the output is
> $ python try.py
> In Main()
> Hi

Now you explicitely call site.main(), so it executes it!

> If I change import site to import sitecustomize the output is as
> above. What gives?

It's normal, this time it's the first time you import it so its content
is executed.

HTH

-- 
Arnaud



More information about the Python-list mailing list