(path, reload=False)
| 74 | |
| 75 | |
| 76 | def _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(""" |
no test coverage detected
searching dependent graphs…