[Tutor] $PYTHONSTARTUP

Hugo Arts hugo.yoshi at gmail.com
Sat Jan 9 01:46:48 CET 2010


On Sat, Jan 9, 2010 at 12:54 AM, Lowell Tackett <lowelltackett at yahoo.com>wrote:

>  I'm not certain if this is more correctly a bash issue, but it seems
> logical to at least start here.  What I hope to do is set some standard
> commands (whenever I open an interactive python shell) as are outlined in
> the Python.org tutorial:
>
> 2.2.4. The Interactive Startup File¶<http://docs.python.org/tutorial/interpreter.html#the-interactive-startup-file>
>
> When you use Python interactively, it is frequently handy to have some
> standard commands executed every time the interpreter is started. You can do
> this by setting an environment variable named *PYTHONSTARTUP*<http://docs.python.org/using/cmdline.html#envvar-PYTHONSTARTUP>to the name of a file containing your start-up commands. This is similar to
> the .profile feature of the Unix shells.
>
> Toward this end, I created a file that I called 'mypythonrc' and expanded
> the $PYTHONSTARTUP [bash] environmental file (in my '.bashrc' file) to
> include it--then I [re]sourced my '~/.bashrc' file to include the new
> entry.  The validity of that step shows up here:
>
> > Fri Jan 08:lowell:Coord]:$echo $PYTHONSTARTUP
> /etc/pythonrc.py:/home/lowell/bash_function/mypythonrc
>
> The details of the file 'mypythonrc' are here:
>
>             1 #!/usr/bin/python
>             2
>             3 import csv
>             4 import os
>             5 filename = os.environ.get('PYTHONSTARTUP')
>             6 if filename and os.path.isfile(filename):
>             7         execfile(filename)
>
> The results are...well, there are none.  The expectation [is] that whenever
> I open up an interactive Python shell the two modules 'csv' and 'os' will be
> "pre"-imported, and waiting for my use.  Ain't happenin'.
>
> So, again...is this the right venue for my question, or does it hafta be
> settled over at one of the (I guess if I hafta, I hafta) Linux forums?
>
> From the virtual desk of Lowell Tackett
>
>
Well, first thing I see wrong with this file, is the execfile call. It makes
no sense. PYTHONSTARTUP equals
"/etc/pythonrc.pyc:/home/lowell/bash_function/mypythonrc". That is not a
valid filename, so the call won't be executed. isfile does not understand
colon-separated paths, it expects a simple filename.

Second, even if the PYTHONSTARTUP did contain a valid filename, and the
contents of that file are as described, this script will execute itself,
resulting in an infinite recursion. The statement makes no sense either way.

The third thing is that PYTHONSTARTUP must be a filename, and only a
filename. It does not understand a colon-separated list either (the
documentation is fairly explicit on this: "if this is the name of a readable
file.."). Trying this on my machine, at least, results in an IOError: no
such file or directory.

remove the execfile call and change PYTHONSTARTUP to just the path of the
one file, and things should work fine.

Hugo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100109/515bb262/attachment.htm>


More information about the Tutor mailing list