Skip to content

Commit

Permalink
load_module function
Browse files Browse the repository at this point in the history
  • Loading branch information
austinschneider committed Jun 6, 2024
1 parent 84083fa commit e0ea77e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions python/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import sys
import uuid
import pathlib
import importlib

THIS_DIR = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -169,6 +170,22 @@ def has_module(module_name):
return True


def load_module(name, path, persist=True):
"""Load a module with a specific name and path"""
url = pathlib.Path(os.path.abspath(path)).as_uri()
module_name = f"{name}-{str(uuid.uuid5(uuid.NAMESPACE_URL, url))}"
if module_name in sys.modules:
return module
spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module_name)
module = sys.modules[module_name]
if not persist:
del sys.modules[module_name]
return module


_VERSION_PATTERN = r"""
v?
(?:
Expand Down Expand Up @@ -556,3 +573,4 @@ def load_flux(model_name, *args, **kwargs):
flux_file = flux.load_flux(*args, **kwargs)
del sys.modules[module_name] # remove flux directory from the system
return flux_file

0 comments on commit e0ea77e

Please sign in to comment.