PYTHONSTARTUP ignored by python -i

David M. Cooke cookedm at
Thu Sep 14 19:52:38 CEST 2000

At some point, hzhu at (Huaiyu Zhu) wrote:

> On 13 Sep 2000 16:59:33 -0400, David M. Cooke <cookedm at>
> wrote: 
> >from the man page:
> >
> >       -i     When a script is passed as first argument or the -c
> >              option is used, enter interactive mode  after  exe­
> >              cuting the script or the command.  It does not read
> >              the $PYTHONSTARTUP file.  This  can  be  useful  to
> >              inspect  global  variables  or a stack trace when a
> >              script raises an exception.
> Wouldn't it be a good idea to have two options, one for whether it's
> interactive, and one for whether to read startup file, with sensible
> defaults of the latter depending on the former?

Good point.

In the meantime, you could use this Python script instead (name
it pythoni or something)

#!/usr/bin/env python
import sys,os
import os.path

modfile = sys.argv[1]
pystartup = os.environ['PYTHONSTARTUP']

env = os.environ.copy()

modpath = os.path.abspath(os.path.dirname(modfile))
if env.has_key('PYTHONPATH'):
    env['PYTHONPATH'] = modpath + ':' + env['PYTHONPATH']
    env['PYTHONPATH'] = modpath

modname, ext = os.path.splitext(os.path.basename(modfile))

os.execvpe("python",["python", "-i", "-c",
           "execfile('%s'); from %s import *"%(pystartup,modname)], env)

Then 'pythoni path/to/script/' will do what you want
(hopefully; this wasn't tested extensively)

David M. Cooke
cookedm at

More information about the Python-list mailing list