Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

site.py confusion

4 views
Skip to first unread message

George Trojan

unread,
Jan 25, 2010, 5:27:19 PM1/25/10
to
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
When I uncomment the site.main() line the output is
$ python try.py
In Main()
Hi
If I change import site to import sitecustomize the output is as above.
What gives?
Adding to the confusion, I found
http://code.activestate.com/recipes/552729/ which contradicts
http://docs.python.org/library/site.html

George

Arnaud Delobelle

unread,
Jan 27, 2010, 12:57:59 PM1/27/10
to
George Trojan <george...@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

George Trojan

unread,
Jan 27, 2010, 1:48:23 PM1/27/10
to
I understand that importing a module repeatedly does nothing. Also, I
made a typo in my previous posting - I meant sitecustomize.main(), not
site.main(). My understanding of the code in site.py is that when the
module is imported, main() is executed. main() calls execsitecustomize()
that attempts to import sitecustomize. That action should trigger
execution of code in sitecustomize.py, which is located in the current
directory. But that does not work. I changed execsitecustomize() to

def execsitecustomize():
"""Run custom site specific code, if available."""
try:
import sitecustomize
except ImportError:
pass
import sys
print sys.path
raise

That gave me the explanation why the above happens: when site is
imported, the current directory is not yet prepended to sys.path.

$ python2.6 -v try.py
...
['/usr/local/Python-2.6.3/lib/python26.zip',
'/usr/local/Python-2.6.3/lib/python2.6',
'/usr/local/Python-2.6.3/lib/python2.6/plat-linux2',
'/usr/local/Python-2.6.3/lib/python2.6/lib-tk',
'/usr/local/Python-2.6.3/lib/python2.6/lib-old',
'/usr/local/Python-2.6.3/lib/python2.6/lib-dynload',
'/usr/local/Python-2.6.3/lib/python2.6/site-packages']
'import site' failed; traceback:
Traceback (most recent call last):
File "/usr/local/Python-2.6.3/lib/python2.6/site.py", line 516, in
<module>
main()
File "/usr/local/Python-2.6.3/lib/python2.6/site.py", line 507, in main
execsitecustomize()
File "/usr/local/Python-2.6.3/lib/python2.6/site.py", line 472, in
execsitecustomize
import sitecustomize
...

This also explains the recipe http://code.activestate.com/recipes/552729/

I wanted to have library location specific to application without having
to explicitly change sys.path in all App-Top-Dir/bin/*.py. I thought
creating bin/sitecustomize.py would do the trick.

I guess the documentation should mention this fact. The comment in
recipe 552729 is:

Since Python 2.5 the automatic import of the module "sitecustomize.py"
in the directory of the main program is not supported any more (even if
the documentation says that it is).

George

Gabriel Genellina

unread,
Jan 29, 2010, 3:18:42 PM1/29/10
to pytho...@python.org
En Wed, 27 Jan 2010 15:48:23 -0300, George Trojan <george...@noaa.gov>
escribi�:

> Arnaud Delobelle wrote:
>> George Trojan <george...@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
>
> That gave me the explanation why the above happens: when site is
> imported, the current directory is not yet prepended to sys.path.
>
> I wanted to have library location specific to application without having
> to explicitly change sys.path in all App-Top-Dir/bin/*.py. I thought
> creating bin/sitecustomize.py would do the trick.

Put sitecustomize.py in any of those directories already in sys.path; on
Python 2.6, you may put sitecustomize.py on your "user site directory",
~/.local/lib/python2.6/site-packages (see PEP370; it won't show up in
sys.path unless the directory actually exists)

--
Gabriel Genellina

0 new messages