MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / run_path

Function run_path

tools/python-3.11.9-amd64/Lib/runpy.py:262–314  ·  view source on GitHub ↗

Execute code located at the specified filesystem location. path_name -- filesystem location of a Python script, zipfile, or directory containing a top level __main__.py script. Optional arguments: init_globals -- dictionary used to pre-populate the module’s

(path_name, init_globals=None, run_name=None)

Source from the content-addressed store, hash-verified

260 return code, fname
261
262def run_path(path_name, init_globals=None, run_name=None):
263 """Execute code located at the specified filesystem location.
264
265 path_name -- filesystem location of a Python script, zipfile,
266 or directory containing a top level __main__.py script.
267
268 Optional arguments:
269 init_globals -- dictionary used to pre-populate the module’s
270 globals dictionary before the code is executed.
271
272 run_name -- if not None, this will be used to set __name__;
273 otherwise, '<run_path>' will be used for __name__.
274
275 Returns the resulting module globals dictionary.
276 """
277 if run_name is None:
278 run_name = "<run_path>"
279 pkg_name = run_name.rpartition(".")[0]
280 from pkgutil import get_importer
281 importer = get_importer(path_name)
282 # Trying to avoid importing imp so as to not consume the deprecation warning.
283 is_NullImporter = False
284 if type(importer).__module__ == 'imp':
285 if type(importer).__name__ == 'NullImporter':
286 is_NullImporter = True
287 if isinstance(importer, type(None)) or is_NullImporter:
288 # Not a valid sys.path entry, so run the code directly
289 # execfile() doesn't help as we want to allow compiled files
290 code, fname = _get_code_from_file(run_name, path_name)
291 return _run_module_code(code, init_globals, run_name,
292 pkg_name=pkg_name, script_name=fname)
293 else:
294 # Finder is defined for path, so add it to
295 # the start of sys.path
296 sys.path.insert(0, path_name)
297 try:
298 # Here's where things are a little different from the run_module
299 # case. There, we only had to replace the module in sys while the
300 # code was running and doing so was somewhat optional. Here, we
301 # have no choice and we have to remove it even while we read the
302 # code. If we don't do this, a __loader__ attribute in the
303 # existing __main__ module may prevent location of the new module.
304 mod_name, mod_spec, code = _get_main_module_details()
305 with _TempModule(run_name) as temp_module, \
306 _ModifiedArgv0(path_name):
307 mod_globals = temp_module.module.__dict__
308 return _run_code(code, mod_globals, init_globals,
309 run_name, mod_spec, pkg_name).copy()
310 finally:
311 try:
312 sys.path.remove(path_name)
313 except ValueError:
314 pass
315
316
317if __name__ == "__main__":

Callers

nothing calls this directly

Calls 12

get_importerFunction · 0.90
_get_code_from_fileFunction · 0.85
_run_module_codeFunction · 0.85
_get_main_module_detailsFunction · 0.85
_TempModuleClass · 0.85
_ModifiedArgv0Class · 0.85
_run_codeFunction · 0.85
rpartitionMethod · 0.80
typeClass · 0.50
insertMethod · 0.45
copyMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected