Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11659ad

Browse files
committed
1. When a module is run from an EditorWindow, if its directory is not in
sys.path, prepend it. This allows the module to import other modules in the same directory. Do the same for a script run from the command line. 2. Tweak the IDLE usage message a bit more. SF Bug 706860 (closed) SF Patch 686254 (reject specific solution) SF Patch 507327 (similar) M PyShell.py M ScriptBinding.py
1 parent 8f570a7 commit 11659ad

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

Lib/idlelib/PyShell.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,18 @@ def stuffsource(self, source):
526526
linecache.cache[filename] = len(source)+1, 0, lines, filename
527527
return filename
528528

529+
def prepend_syspath(self, filename):
530+
"Prepend sys.path with file's directory if not already included"
531+
self.runcommand("""if 1:
532+
_filename = %s
533+
import sys as _sys
534+
from os.path import dirname as _dirname
535+
_dir = _dirname(_filename)
536+
if not _dir in _sys.path:
537+
_sys.path.insert(0, _dir)
538+
del _filename, _sys, _dirname, _dir
539+
\n""" % `filename`)
540+
529541
def showsyntaxerror(self, filename=None):
530542
"""Extend base class method: Add Colorizing
531543
@@ -1069,9 +1081,9 @@ def isatty(self):
10691081

10701082
usage_msg = """\
10711083
1072-
USAGE: idle [-deis] [-t title] [file]*
1073-
idle [-ds] [-t title] (-c cmd | -r file) [arg]*
1074-
idle [-ds] [-t title] - [arg]*
1084+
USAGE: idle [-deins] [-t title] [file]*
1085+
idle [-dns] [-t title] (-c cmd | -r file) [arg]*
1086+
idle [-dns] [-t title] - [arg]*
10751087
10761088
-h print this help message and exit
10771089
-n run IDLE without a subprocess (see Help/IDLE Help for details)
@@ -1234,6 +1246,7 @@ def main():
12341246
if cmd:
12351247
shell.interp.execsource(cmd)
12361248
elif script:
1249+
shell.interp.prepend_syspath(script)
12371250
shell.interp.execfile(script)
12381251
root.mainloop()
12391252
root.destroy()

Lib/idlelib/ScriptBinding.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ def run_module_event2(self, interp, filename, code):
144144
if (not _sys.argv or
145145
_basename(_sys.argv[0]) != _basename(_filename)):
146146
_sys.argv = [_filename]
147-
del _filename, _sys, _basename
148-
\n""" % `filename`)
147+
del _filename, _sys, _basename
148+
\n""" % `filename`)
149+
interp.prepend_syspath(filename)
149150
interp.runcode(code)
150151

151152
def getfilename(self):

0 commit comments

Comments
 (0)