MCPcopy Index your code
hub / github.com/python/cpython / run_path

Function run_path

Lib/runpy.py:260–308  ·  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 globa

(path_name, init_globals=None, run_name=None)

Source from the content-addressed store, hash-verified

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

Callers 3

create_nsMethod · 0.90
test_encodingMethod · 0.90

Calls 11

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.45
insertMethod · 0.45
copyMethod · 0.45
removeMethod · 0.45

Tested by 3

create_nsMethod · 0.72
test_encodingMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…