site.py confusion

G

George Trojan

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
 
A

Arnaud Delobelle

George Trojan said:
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
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
 
G

George Trojan

Arnaud said:
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


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


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

HTH
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
 
G

Gabriel Genellina

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top