[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: python startup files



"Jens B. Jorgensen" <jjorgens@bdsinc.com> writes:

> I dunno about .pythonrc.py. The docs I read said to put startup code in
> ~/.python-startup.py and this is what I use successfully. In order to modify python's
> search path from python code you just need to manipulate the sys.path list, eg.:
> 
> import sys
> 
> sys.path.append('path-to-append')

Hmm.  That is what I was trying to do.  It doesn't seem to work for me
under the name ~/.python-startup.py either.

 $ cat ~/.python-startup.py
 import rlcompleter, readline, sys

 readline.parse_and_bind('tab: complete')
 sys.path.append("/home/bates/src/python")
 $ python
 Python 1.5.1 (#1, Dec 17 1998, 20:58:15)  [GCC 2.7.2.3] on linux2
 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
 >>> dir()        # notice that sys has not been imported
 ['__builtins__', '__doc__', '__name__']
 >>> sys.path
 Traceback (innermost last):
   File "<stdin>", line 1, in ?
 NameError: sys
 >>> ^D

> In order to augment the python search path from an environment
> variable just set PYTHONPATH (colon-separated list just as the PATH
> variable). This is in the man page.  Did you read the fine manual?

Well I have been reading a lot of python documentation but I am
willing to believe I have missed important parts :-).  

On closer inspection of the manual page I see that the directories in
PYTHONPATH are prepended to the standard path list for python.  My
(poorly phrased) question about adding to the search list was more a
question of whether PYTHONPATH is appended to the standard search
list, is prepended to the standard search list, or replaces the
standard search list.  You are right - I could have gotten the answer
from the manual page.

It also appears that you need to set the environment variable
PYTHONSTARTUP to the name of the file that will be used for
initialization, whether that is ~/.pythonrc.py or
~/.python-startup.py.  My standard shell startup doesn't set this
environment variable but I will change my ~/.bash-profile to do so.

 $ export PYTHONSTARTUP=/home/bates/.python-startup.py
 $ python
 Python 1.5.1 (#1, Dec 17 1998, 20:58:15)  [GCC 2.7.2.3] on linux2
 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
 >>> dir()
 ['__builtins__', '__doc__', '__name__', 'readline', 'rlcompleter', 'sys']
 >>> ^D

I think I have the answers now for my questions.  Thanks for the
help.

> Douglas Bates wrote:
> 
> > According to some python documentation I have read I should put
> > commands that I want executed in every interactive python session into
> > the file `~/.pythonrc.py'.  I have done that but these commands do not
> > seem to be executed when I start python on a Debian GNU/Linux 2.1
> > system with
> > ||/ Name            Version        Description
> > +++-===============-==============-============================================
> > ii  python-base     1.5.1-7        An interactive object-oriented scripting lan
> >
> > Is there another convention for python startup with this package?
> >
> > Also, what is the recommended form for adding to the python search
> > path?  I believe there is an environment variable to set.
> >
> > Please cc: me on replies.  I am unable to keep up with the amount of
> > traffic on this list.
> --
> Jens B. Jorgensen
> jjorgens@bdsinc.com

-- 
Douglas Bates                            bates@stat.wisc.edu
Statistics Department                    608/262-2598
University of Wisconsin - Madison        http://www.stat.wisc.edu/~bates/


Reply to: