diff Lib/idlelib/PyShell.py @ 29122:c7dfda993b2f legacy-trunk

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
author Kurt B. Kaiser <kbk@shore.net>
date Thu, 15 May 2003 23:23:21 +0000
parents 29f19460f067
children 9cf09f92b198
line wrap: on
line diff
--- a/Lib/idlelib/PyShell.py	Thu May 15 18:52:51 2003 +0000
+++ b/Lib/idlelib/PyShell.py	Thu May 15 23:23:21 2003 +0000
@@ -526,6 +526,18 @@
         linecache.cache[filename] = len(source)+1, 0, lines, filename
         return filename
 
+    def prepend_syspath(self, filename):
+        "Prepend sys.path with file's directory if not already included"
+        self.runcommand("""if 1:
+            _filename = %s
+            import sys as _sys
+            from os.path import dirname as _dirname
+            _dir = _dirname(_filename)
+            if not _dir in _sys.path:
+                _sys.path.insert(0, _dir)
+            del _filename, _sys, _dirname, _dir
+            \n""" % `filename`)
+
     def showsyntaxerror(self, filename=None):
         """Extend base class method: Add Colorizing
 
@@ -1069,9 +1081,9 @@
 
 usage_msg = """\
 
-USAGE: idle  [-deis] [-t title] [file]*
-       idle  [-ds] [-t title] (-c cmd | -r file) [arg]*
-       idle  [-ds] [-t title] - [arg]*
+USAGE: idle  [-deins] [-t title] [file]*
+       idle  [-dns] [-t title] (-c cmd | -r file) [arg]*
+       idle  [-dns] [-t title] - [arg]*
 
   -h         print this help message and exit
   -n         run IDLE without a subprocess (see Help/IDLE Help for details)
@@ -1234,6 +1246,7 @@
         if cmd:
             shell.interp.execsource(cmd)
         elif script:
+            shell.interp.prepend_syspath(script)
             shell.interp.execfile(script)
     root.mainloop()
     root.destroy()