[Idle-dev] [ idlefork-Patches-507327 ] ScriptBinding.py auto modify sys.path

noreply@sourceforge.net noreply@sourceforge.net
Tue, 22 Jan 2002 19:01:49 -0800


Patches item #507327, was opened at 2002-01-22 19:01
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=309579&aid=507327&group_id=9579

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Stephen M. Gava (elguavas)
Assigned to: Nobody/Anonymous (nobody)
Summary: ScriptBinding.py auto modify sys.path

Initial Comment:
patch submitted to idle-dev by 	
Danny Yoo <dyoo@hkn.eecs.berkeley.edu>

although this applies to ScriptBinding.py it may be of
some use in future separate process execution stuff

original message to idle-dev:
-----------------------------

Hi everyone,

A while back, I sent a patch to automagically add a
script's directory
path to sys.path.  Here's a link to the original post:

   
http://mail.python.org/pipermail/tutor/2001-December/010915.html


The problem is this: if a user is experimenting with
importing their own
modules, they run into an ImportError because their
modules aren't located
in the system path.

This is somewhat weird to me, as I would have expected
IDLE to do this for
me.  I've isolated the code to run_script_event(),
which uses an
execfile() call.  execfile() doesn't extend sys.path,
probably for
security reasons.  But for someone running scripts
through IDLE, this
seems a bit paranoid.

Here's a modified run_script_even that temporarily
adjusts sys.path, just
long enough for the run_script command to finish:

####
def run_script_event(self, event):
    filename = self.getfilename()
    if not filename:
        return

    flist = self.editwin.flist
    shell = flist.open_shell()
    interp = shell.interp
    if (not sys.argv or
        os.path.basename(sys.argv[0]) !=
os.path.basename(filename)):
        # XXX Too often this discards arguments the
user just set...
        sys.argv = [filename]
    old_syspath = sys.path[:]
   
sys.path.append(os.path.dirname(os.path.abspath(filename)))
    interp.execfile(filename)
    sys.path = old_syspath
###


Does this seem like a reasonable thing to add to IDLE? 
I always feel very
worried about telling new Python programmers to edit
their PYTHONPATH...  
*grin*


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=309579&aid=507327&group_id=9579