MCPcopy Index your code
hub / github.com/modelscope/modelscope / import_module_from_model_dir

Function import_module_from_model_dir

modelscope/utils/plugins.py:377–414  ·  view source on GitHub ↗

import all the necessary module from a model dir Args: model_dir: model file location No returns, raise error if failed

(model_dir)

Source from the content-addressed store, hash-verified

375
376
377def import_module_from_model_dir(model_dir):
378 """ import all the necessary module from a model dir
379
380 Args:
381 model_dir: model file location
382
383 No returns, raise error if failed
384
385 """
386 from pathlib import Path
387 file_scanner = FilesAstScanning()
388 file_scanner.traversal_files(model_dir, include_init=True)
389 file_dirs = file_scanner.file_dirs
390 requirements = file_scanner.requirement_dirs
391
392 # install the requirements firstly
393 install_requirements_by_files(requirements)
394
395 if BASE_MODULE_DIR not in sys.path:
396 sys.path.append(BASE_MODULE_DIR)
397
398 module_name = Path(model_dir).stem
399
400 # in order to keep forward compatibility, we add module path to
401 # sys.path so that submodule can be imported directly as before
402 MODULE_PATH = os.path.join(BASE_MODULE_DIR, module_name)
403 if MODULE_PATH not in sys.path:
404 sys.path.append(MODULE_PATH)
405
406 relative_file_dirs = [
407 file.replace(model_dir.rstrip(os.sep) + os.sep, '')
408 for file in file_dirs
409 ]
410 create_module_from_files(relative_file_dirs, model_dir, module_name)
411 for file in relative_file_dirs:
412 submodule = module_name + '.' + file.replace('.py', '').replace(
413 os.sep, '.')
414 importlib.import_module(submodule)
415
416
417def install_requirements_by_names(plugins: List[str]):

Callers 1

register_modelhub_repoFunction · 0.85

Calls 7

traversal_filesMethod · 0.95
FilesAstScanningClass · 0.90
create_module_from_filesFunction · 0.85
replaceMethod · 0.80
import_moduleMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…