[Idle-dev] [ idlefork-Patches-507327 ] ScriptBinding.py auto modify sys.path
SourceForge.net
noreply@sourceforge.net
Thu, 15 May 2003 16:24:50 -0700
Patches item #507327, was opened at 2002-01-22 22:01
Message generated for change (Comment added) made by kbk
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=309579&aid=507327&group_id=9579
Category: None
Group: None
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Stephen M. Gava (elguavas)
>Assigned to: Kurt B. Kaiser (kbk)
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*
----------------------------------------------------------------------
>Comment By: Kurt B. Kaiser (kbk)
Date: 2003-05-15 18:24
Message:
Logged In: YES
user_id=149084
ScriptBinding Rev 1.20
PyShell Rev 1.67
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=309579&aid=507327&group_id=9579