Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 1 | """runpy.py - locating and running Python code using the module namespace |
| 2 | |
| 3 | Provides support for locating and running Python scripts using the Python |
| 4 | module namespace instead of the native filesystem. |
| 5 | |
| 6 | This allows Python code to play nicely with non-filesystem based PEP 302 |
| 7 | importers when locating support scripts as well as when importing modules. |
| 8 | """ |
| 9 | # Written by Nick Coghlan <ncoghlan at gmail.com> |
| 10 | # to implement PEP 338 (Executing Modules as Scripts) |
| 11 | |
Brett Cannon | aa93642 | 2012-04-27 15:30:58 -0400 | [diff] [blame] | 12 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 13 | import sys |
Nick Coghlan | be7e49f | 2012-07-20 23:40:09 +1000 | [diff] [blame] | 14 | import importlib.machinery # importlib first so we can test #15386 via -m |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 15 | import importlib.util |
Brett Cannon | 82d2107 | 2013-06-15 14:27:21 -0400 | [diff] [blame] | 16 | import types |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 17 | from pkgutil import read_code, get_importer |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 18 | |
| 19 | __all__ = [ |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 20 | "run_module", "run_path", |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 21 | ] |
| 22 | |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 23 | class _TempModule(object): |
| 24 | """Temporarily replace a module in sys.modules with an empty namespace""" |
| 25 | def __init__(self, mod_name): |
| 26 | self.mod_name = mod_name |
Brett Cannon | 82d2107 | 2013-06-15 14:27:21 -0400 | [diff] [blame] | 27 | self.module = types.ModuleType(mod_name) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 28 | self._saved_module = [] |
| 29 | |
| 30 | def __enter__(self): |
| 31 | mod_name = self.mod_name |
| 32 | try: |
| 33 | self._saved_module.append(sys.modules[mod_name]) |
| 34 | except KeyError: |
| 35 | pass |
| 36 | sys.modules[mod_name] = self.module |
| 37 | return self |
| 38 | |
| 39 | def __exit__(self, *args): |
| 40 | if self._saved_module: |
| 41 | sys.modules[self.mod_name] = self._saved_module[0] |
| 42 | else: |
| 43 | del sys.modules[self.mod_name] |
| 44 | self._saved_module = [] |
| 45 | |
| 46 | class _ModifiedArgv0(object): |
| 47 | def __init__(self, value): |
| 48 | self.value = value |
| 49 | self._saved_value = self._sentinel = object() |
| 50 | |
| 51 | def __enter__(self): |
| 52 | if self._saved_value is not self._sentinel: |
| 53 | raise RuntimeError("Already preserving saved value") |
| 54 | self._saved_value = sys.argv[0] |
| 55 | sys.argv[0] = self.value |
| 56 | |
| 57 | def __exit__(self, *args): |
| 58 | self.value = self._sentinel |
| 59 | sys.argv[0] = self._saved_value |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 60 | |
Eric Snow | 32439d6 | 2015-05-02 19:15:18 -0600 | [diff] [blame] | 61 | # TODO: Replace these helpers with importlib._bootstrap_external functions. |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 62 | def _run_code(code, run_globals, init_globals=None, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 63 | mod_name=None, mod_spec=None, |
| 64 | pkg_name=None, script_name=None): |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 65 | """Helper to run code in nominated namespace""" |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 66 | if init_globals is not None: |
| 67 | run_globals.update(init_globals) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 68 | if mod_spec is None: |
| 69 | loader = None |
| 70 | fname = script_name |
| 71 | cached = None |
| 72 | else: |
| 73 | loader = mod_spec.loader |
| 74 | fname = mod_spec.origin |
| 75 | cached = mod_spec.cached |
| 76 | if pkg_name is None: |
| 77 | pkg_name = mod_spec.parent |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 78 | run_globals.update(__name__ = mod_name, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 79 | __file__ = fname, |
| 80 | __cached__ = cached, |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 81 | __doc__ = None, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 82 | __loader__ = loader, |
| 83 | __package__ = pkg_name, |
| 84 | __spec__ = mod_spec) |
Georg Brandl | 7cae87c | 2006-09-06 06:51:57 +0000 | [diff] [blame] | 85 | exec(code, run_globals) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 86 | return run_globals |
| 87 | |
| 88 | def _run_module_code(code, init_globals=None, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 89 | mod_name=None, mod_spec=None, |
| 90 | pkg_name=None, script_name=None): |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 91 | """Helper to run code in new namespace with sys modified""" |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 92 | fname = script_name if mod_spec is None else mod_spec.origin |
| 93 | with _TempModule(mod_name) as temp_module, _ModifiedArgv0(fname): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 94 | mod_globals = temp_module.module.__dict__ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 95 | _run_code(code, mod_globals, init_globals, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 96 | mod_name, mod_spec, pkg_name, script_name) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 97 | # Copy the globals of the temporary module, as they |
| 98 | # may be cleared when the temporary module goes away |
| 99 | return mod_globals.copy() |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 100 | |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 101 | # Helper to get the loader, code and filename for a module |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 102 | def _get_module_details(mod_name, error=ImportError): |
Martin Panter | 7dda421 | 2015-12-10 06:47:06 +0000 | [diff] [blame] | 103 | if mod_name.startswith("."): |
| 104 | raise error("Relative module names not supported") |
| 105 | pkg_name, _, _ = mod_name.rpartition(".") |
| 106 | if pkg_name: |
| 107 | # Try importing the parent to avoid catching initialization errors |
| 108 | try: |
| 109 | __import__(pkg_name) |
| 110 | except ImportError as e: |
| 111 | # If the parent or higher ancestor package is missing, let the |
| 112 | # error be raised by find_spec() below and then be caught. But do |
| 113 | # not allow other errors to be caught. |
| 114 | if e.name is None or (e.name != pkg_name and |
| 115 | not pkg_name.startswith(e.name + ".")): |
| 116 | raise |
| 117 | |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 118 | try: |
Eric Snow | 6029e08 | 2014-01-25 15:32:46 -0700 | [diff] [blame] | 119 | spec = importlib.util.find_spec(mod_name) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 120 | except (ImportError, AttributeError, TypeError, ValueError) as ex: |
| 121 | # This hack fixes an impedance mismatch between pkgutil and |
| 122 | # importlib, where the latter raises other errors for cases where |
| 123 | # pkgutil previously raised ImportError |
| 124 | msg = "Error while finding spec for {!r} ({}: {})" |
Martin Panter | 7dda421 | 2015-12-10 06:47:06 +0000 | [diff] [blame] | 125 | raise error(msg.format(mod_name, type(ex).__name__, ex)) from ex |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 126 | if spec is None: |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 127 | raise error("No module named %s" % mod_name) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 128 | if spec.submodule_search_locations is not None: |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 129 | if mod_name == "__main__" or mod_name.endswith(".__main__"): |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 130 | raise error("Cannot use package as __main__ module") |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 131 | try: |
| 132 | pkg_main_name = mod_name + ".__main__" |
Martin Panter | 7dda421 | 2015-12-10 06:47:06 +0000 | [diff] [blame] | 133 | return _get_module_details(pkg_main_name, error) |
| 134 | except error as e: |
Martin Panter | dda5843 | 2015-12-12 06:58:55 +0000 | [diff] [blame] | 135 | if mod_name not in sys.modules: |
| 136 | raise # No module loaded; being a package is irrelevant |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 137 | raise error(("%s; %r is a package and cannot " + |
Nick Coghlan | 3f48ae3 | 2009-02-08 01:58:26 +0000 | [diff] [blame] | 138 | "be directly executed") %(e, mod_name)) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 139 | loader = spec.loader |
| 140 | if loader is None: |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 141 | raise error("%r is a namespace package and cannot be executed" |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 142 | % mod_name) |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 143 | try: |
| 144 | code = loader.get_code(mod_name) |
| 145 | except ImportError as e: |
| 146 | raise error(format(e)) from e |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 147 | if code is None: |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 148 | raise error("No code object available for %s" % mod_name) |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 149 | return mod_name, spec, code |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 150 | |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 151 | class _Error(Exception): |
| 152 | """Error that _run_module_as_main() should report without a traceback""" |
| 153 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 154 | # XXX ncoghlan: Should this be documented and made public? |
| 155 | # (Current thoughts: don't repeat the mistake that lead to its |
| 156 | # creation when run_module() no longer met the needs of |
| 157 | # mainmodule.c, but couldn't be changed because it was public) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 158 | def _run_module_as_main(mod_name, alter_argv=True): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 159 | """Runs the designated module in the __main__ namespace |
| 160 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 161 | Note that the executed module will have full access to the |
| 162 | __main__ namespace. If this is not desirable, the run_module() |
R. David Murray | 445448c | 2009-12-20 17:28:31 +0000 | [diff] [blame] | 163 | function should be used to run the module code in a fresh namespace. |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 164 | |
| 165 | At the very least, these variables in __main__ will be overwritten: |
| 166 | __name__ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 167 | __file__ |
Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 168 | __cached__ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 169 | __loader__ |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 170 | __package__ |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 171 | """ |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 172 | try: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 173 | if alter_argv or mod_name != "__main__": # i.e. -m switch |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 174 | mod_name, mod_spec, code = _get_module_details(mod_name, _Error) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 175 | else: # i.e. directory or zipfile execution |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 176 | mod_name, mod_spec, code = _get_main_module_details(_Error) |
| 177 | except _Error as exc: |
| 178 | msg = "%s: %s" % (sys.executable, exc) |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 179 | sys.exit(msg) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 180 | main_globals = sys.modules["__main__"].__dict__ |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 181 | if alter_argv: |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 182 | sys.argv[0] = mod_spec.origin |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 183 | return _run_code(code, main_globals, None, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 184 | "__main__", mod_spec) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 185 | |
| 186 | def run_module(mod_name, init_globals=None, |
| 187 | run_name=None, alter_sys=False): |
| 188 | """Execute a module's code without importing it |
| 189 | |
| 190 | Returns the resulting top level namespace dictionary |
| 191 | """ |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 192 | mod_name, mod_spec, code = _get_module_details(mod_name) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 193 | if run_name is None: |
| 194 | run_name = mod_name |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 195 | if alter_sys: |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 196 | return _run_module_code(code, init_globals, run_name, mod_spec) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 197 | else: |
| 198 | # Leave the sys module alone |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 199 | return _run_code(code, {}, init_globals, run_name, mod_spec) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 200 | |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 201 | def _get_main_module_details(error=ImportError): |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 202 | # Helper that gives a nicer error message when attempting to |
| 203 | # execute a zipfile or directory by invoking __main__.py |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 204 | # Also moves the standard __main__ out of the way so that the |
| 205 | # preexisting __loader__ entry doesn't cause issues |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 206 | main_name = "__main__" |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 207 | saved_main = sys.modules[main_name] |
| 208 | del sys.modules[main_name] |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 209 | try: |
| 210 | return _get_module_details(main_name) |
| 211 | except ImportError as exc: |
| 212 | if main_name in str(exc): |
Martin Panter | 657257e | 2015-12-03 01:23:10 +0000 | [diff] [blame] | 213 | raise error("can't find %r module in %r" % |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 214 | (main_name, sys.path[0])) from exc |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 215 | raise |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 216 | finally: |
| 217 | sys.modules[main_name] = saved_main |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 218 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 219 | |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 220 | def _get_code_from_file(run_name, fname): |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 221 | # Check for a compiled file first |
| 222 | with open(fname, "rb") as f: |
| 223 | code = read_code(f) |
| 224 | if code is None: |
| 225 | # That didn't work, so try it as normal source code |
Victor Stinner | 6c47102 | 2011-07-04 01:45:39 +0200 | [diff] [blame] | 226 | with open(fname, "rb") as f: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 227 | code = compile(f.read(), fname, 'exec') |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 228 | return code, fname |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 229 | |
| 230 | def run_path(path_name, init_globals=None, run_name=None): |
| 231 | """Execute code located at the specified filesystem location |
| 232 | |
| 233 | Returns the resulting top level namespace dictionary |
| 234 | |
| 235 | The file path may refer directly to a Python script (i.e. |
| 236 | one that could be directly executed with execfile) or else |
| 237 | it may refer to a zipfile or directory containing a top |
| 238 | level __main__.py script. |
| 239 | """ |
| 240 | if run_name is None: |
| 241 | run_name = "<run_path>" |
Nick Coghlan | 761bb11 | 2012-07-14 23:59:22 +1000 | [diff] [blame] | 242 | pkg_name = run_name.rpartition(".")[0] |
Nick Coghlan | 85e729e | 2012-07-15 18:09:52 +1000 | [diff] [blame] | 243 | importer = get_importer(path_name) |
Brett Cannon | e4f41de | 2013-06-16 13:13:40 -0400 | [diff] [blame] | 244 | # Trying to avoid importing imp so as to not consume the deprecation warning. |
| 245 | is_NullImporter = False |
| 246 | if type(importer).__module__ == 'imp': |
| 247 | if type(importer).__name__ == 'NullImporter': |
| 248 | is_NullImporter = True |
| 249 | if isinstance(importer, type(None)) or is_NullImporter: |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 250 | # Not a valid sys.path entry, so run the code directly |
| 251 | # execfile() doesn't help as we want to allow compiled files |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 252 | code, fname = _get_code_from_file(run_name, path_name) |
| 253 | return _run_module_code(code, init_globals, run_name, |
| 254 | pkg_name=pkg_name, script_name=fname) |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 255 | else: |
| 256 | # Importer is defined for path, so add it to |
| 257 | # the start of sys.path |
| 258 | sys.path.insert(0, path_name) |
| 259 | try: |
| 260 | # Here's where things are a little different from the run_module |
| 261 | # case. There, we only had to replace the module in sys while the |
| 262 | # code was running and doing so was somewhat optional. Here, we |
| 263 | # have no choice and we have to remove it even while we read the |
| 264 | # code. If we don't do this, a __loader__ attribute in the |
| 265 | # existing __main__ module may prevent location of the new module. |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 266 | mod_name, mod_spec, code = _get_main_module_details() |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 267 | with _TempModule(run_name) as temp_module, \ |
| 268 | _ModifiedArgv0(path_name): |
| 269 | mod_globals = temp_module.module.__dict__ |
| 270 | return _run_code(code, mod_globals, init_globals, |
Nick Coghlan | 720c7e2 | 2013-12-15 20:33:02 +1000 | [diff] [blame] | 271 | run_name, mod_spec, pkg_name).copy() |
Nick Coghlan | 260bd3e | 2009-11-16 06:49:25 +0000 | [diff] [blame] | 272 | finally: |
| 273 | try: |
| 274 | sys.path.remove(path_name) |
| 275 | except ValueError: |
| 276 | pass |
| 277 | |
| 278 | |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 279 | if __name__ == "__main__": |
| 280 | # Run the module specified as the next command line argument |
| 281 | if len(sys.argv) < 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 282 | print("No module specified for execution", file=sys.stderr) |
Thomas Wouters | a977329 | 2006-04-21 09:43:23 +0000 | [diff] [blame] | 283 | else: |
| 284 | del sys.argv[0] # Make the requested module sys.argv[0] |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 285 | _run_module_as_main(sys.argv[0]) |