[Tutor] Re: [Pythonmac-SIG] sitecustomize and pythonstartup

Jack Jansen Jack.Jansen@oratrix.nl
Sat, 2 Feb 2002 00:16:44 +0100


There's some good info in your piece, I would really like it if 
it could be turned into something (customizing.html?) that could 
go into the documentation at some point.

Some comments:

On Friday, February 1, 2002, at 11:26  PM, Christopher Smith wrote:

> I've been trying to learn what behaviors can be automated at 
> start up and
> have come up with the following which might be useful for others.
> Corrections are welcome.  It also raises a question that I ask below.
>
> ---
>
> _____
> A script can
>
> 	import user
>
> which will run the script ".pythonrc.py" if it exists in the same
> directory as the script.  Objects in .pythonrc.py (imported modules,
> function definitions, or variables) are accessible to the 
> calling script
> by using the "user." prefix.  For example, if math has been loaded in
> .pythonrc.py, the main program can access math.pi as user.math.pi  {see
> user.py}
>
> _____
> The Python IDE will attempt to load a file named 
> sitecustomize.pyc if it
> exists on the paths defined by sys.path.  No objects therein (modules,
> functions, variables) are accessible to the IDE interactive console or
> scripts, but changes to variables like sys.path can be made. If 
> there is
> any printing done in the file, this will create a window titled "Python
> IDE.out" which can be moved and resized but not closed.  {see 
> site.py and
> the file sitecustomize.py}

Actually sitecustomize.py works just as well as .pyc. The 
sitecustomize module is simply imported (by site.py, which is in 
it's turn "magically" imported during startup of every 
interpreter, interactive, running a script or applet (such as 
IDE).

The bit about the Python IDE.out window is a bug, really: 
sitecustomize is imported before the IDE is running, so the 
output is going to the "normal" PythonInterpreter output window, 
which is normally suppressed. Unfortunately it is a bug that is 
difficult to fix (because fixing it would mean that IDE crashes 
resulted in no stack trace).

> _____
> The Python Interpreter will attempt to run the 
> sitecustomize.pyc script if
> it exists on the paths defined by sys.path.  No objects therein 
> (modules,
> functions, variables) are accessible to the IDE interactive console or
> scripts.
>
> The Interpreter will also attempt to import and run the 
> contents of a file
> named PythonStartup if it exists on in the same directory as the
> interpreter.  Objects in PythonStartup are then accessible 
> directly to the
> interpreter (e.g. if "import math" appears in PythonStartup then typing
> "math.pi" in the interpreter will yield the value of pi).  
> Commands like
> "from __future__ import division" will not affect the Interpreter,
> however, and must be reimported there.  If PythonStartup 
> imports a module
> A which in turn imports a module B, B's namespace must be accessed as
> A.B.name. {see section "Interactive startup file" in the 
> using.html file
> in the Mac:Demo folder.
>
>
>
>
> -------
>
> OK, the question is this.  The interpreter can be educated as to what
> modules are loaded (i.e. if pythonstartup imports math, you can type
> math.pi in the interpreter and be understood).  Is there a way 
> to get this
> to happen in the IDE's interpreter as well?  If 
> sitecustomize.py imports
> math I can't seem to access that at the >>> prompt.

This would be A Good Thing. Maybe you can submit it as a feature 
request in the sourceforge tracker? You can assign it to jvr, 
which is Just:-)

> Also, is there a way to get "from __future__ import division" to be the
> default behavior in the interpreter?  This statement in the 
> startup file
> does not create the floating integer behavior in the IDE's 
> interpreter or
> in the Python Interpreter itself.

Hmm, I there may be a good reason for this, but it might also be 
an oversight. You definitely don't want to enable it globally 
(as this would break lots of standard modules) but I can't think 
of a reason why it can't be enabled for the interactive 
interpreter in PythonStartup (or the unix-equivalent 
PYTHONSTARTUP environment variable). Maybe submit this as a 
feature request (or bug report) too? I'm not sure who you should 
assign this to, probably Guido.
--
- Jack Jansen        <Jack.Jansen@oratrix.com>        
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- 
Emma Goldman -