Member-only story

Module Replacement in Python On The Fly

dboost.me
2 min readNov 24, 2023

Sometimes bad things happen:

  • libraries can be inefficient
  • libraries may depend on libraries which are inefficient

We may create a more effective solution make it compatible with the existing one and replace it in the runtime. Let’s figure out how we can do it.

Useful terminology

  1. sys.path — a list of strings that specifies the search path from modules
  2. sys.meta_path — a list of “meta path finder” objects, that are responsible for searching a module

Importing module manually

Loaded modules are cached in sys.modules variable, so we can check if our load was succesfull. Let’s load uuid module as an experiment. Modules are loaded by calling find_spec on a finder object — if the result is not none, then the corresponding loader can find the module. In our case the last one _frozen_importlib_external.PathFinder from the sys.meta_path is the one who can find the standart uuid module.

path_finder = sys.meta_path[4]
print(path_finder)

spec = path_finder.find_spec("uuid", None)
print(spec)

uuid_before = sys.modules.get("uuid")
print(f"uuid before -> {uuid_before}")

module = spec.loader.load_module(spec.name)

uuid_after = sys.modules.get("uuid")…

--

--

No responses yet