MCPcopy Index your code
hub / github.com/pywebio/PyWebIO / _get_module

Function _get_module

pywebio/platform/path_deploy.py:76–102  ·  view source on GitHub ↗
(path, reload=False)

Source from the content-addressed store, hash-verified

74
75
76def _get_module(path, reload=False):
77 # https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
78 # https://stackoverflow.com/questions/41861427/python-3-5-how-to-dynamically-import-a-module-given-the-full-file-path-in-the
79 global _cached_modules
80 import importlib.util
81
82 @contextmanager
83 def add_to_path(p):
84 import sys
85 sys.path.append(p)
86 try:
87 yield
88 finally:
89 sys.path.remove(p)
90
91 if not reload and path in _cached_modules:
92 return _cached_modules[path]
93
94 # import_name will be the `__name__` of the imported module
95 import_name = "__pywebio__"
96
97 with add_to_path(os.path.dirname(path)):
98 spec = importlib.util.spec_from_file_location(import_name, path, submodule_search_locations=None)
99 module = importlib.util.module_from_spec(spec)
100 spec.loader.exec_module(module)
101 _cached_modules[path] = module
102 return module
103
104
105_app_list_tpl = tornado.template.Template("""

Callers 1

get_app_from_pathFunction · 0.85

Calls 1

add_to_pathFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…