Skip to content

module_utils

Dynamic module loading, importing, and introspection utilities.

This module provides functions for discovering, loading, installing, and manipulating Python modules at runtime. It supports loading modules from filesystem paths, resolving module hierarchies, importing module attributes by dotted path, and cloning modules.

Example
import os
import tempfile
import py_misc_utils.module_utils as mu

tdir = tempfile.TemporaryDirectory()
mfile = os.path.join(tdir.name, 'zapmod.py')
with open(mfile, 'w') as fpt:
  fpt.write('V = 42')

# Load a module directly from its file path...
mod = mu.load_module(mfile)
assert mod.V == 42

# ...or through the import_module() dispatcher.
mod = mu.import_module(mfile)
assert mod.V == 42

tdir.cleanup()

split_module_name(name)

Split a dotted module name into (parent, child) components.

Parameters:

Name Type Description Default
name

The dotted module name string (e.g. 'a.b.c').

required

Returns:

Type Description

A tuple (parent, child) where parent is everything before the last dot

(or '' if there is no dot) and child is the part after the last dot

(or the whole name if there is no dot).

Source code in py_misc_utils/module_utils.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def split_module_name(name):
  """Split a dotted module name into (parent, child) components.

  Args:
    name: The dotted module name string (e.g. 'a.b.c').

  Returns:
    A tuple (parent, child) where parent is everything before the last dot
    (or '' if there is no dot) and child is the part after the last dot
    (or the whole name if there is no dot).
  """
  pos = name.rfind('.')
  if pos < 0:
    return '', name

  return name[:pos], name[pos + 1:]

add_sys_path(path)

Add a path to sys.path if it is not already present.

Parameters:

Name Type Description Default
path

The filesystem path string to add to sys.path.

required
Source code in py_misc_utils/module_utils.py
63
64
65
66
67
68
69
def add_sys_path(path):
  """Add a path to sys.path if it is not already present.

  Args:
    path: The filesystem path string to add to sys.path.
  """
  cu.append_if_missing(sys.path, path)

find_module_parent(path)

Find the parent package init.py and remaining path parts for a module path.

Walks up the directory tree from the given path looking for init.py files to determine the package parent and the relative module path within it.

Parameters:

Name Type Description Default
path

The filesystem path to a module file.

required

Returns:

Type Description

A tuple (init_path, remaining_parts) where init_path is the path to the

nearest parent init.py and remaining_parts is a list of path components

from that parent to the module name (without .py extension). Returns None

if no parent package is found.

Source code in py_misc_utils/module_utils.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def find_module_parent(path):
  """Find the parent package __init__.py and remaining path parts for a module path.

  Walks up the directory tree from the given path looking for __init__.py files
    to determine the package parent and the relative module path within it.

  Args:
    path: The filesystem path to a module file.

  Returns:
    A tuple (init_path, remaining_parts) where init_path is the path to the
    nearest parent __init__.py and remaining_parts is a list of path components
    from that parent to the module name (without .py extension). Returns None
    if no parent package is found.
  """
  parts = fsu.path_split(path)
  modname = fsu.drop_ext(parts.pop(), '.py')

  for i in range(len(parts)):
    ipath = os.path.join(*parts[:i + 1], _PYINIT)
    if os.path.isfile(ipath):
      return ipath, parts[i + 1:] + [modname]

find_module(path)

Find an already-imported module by its filesystem path.

Parameters:

Name Type Description Default
path

The filesystem path to search for in sys.modules.

required

Returns:

Type Description

A tuple (modname, module) if a module with the given absolute path is found

in sys.modules, or None if no such module exists.

Source code in py_misc_utils/module_utils.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def find_module(path):
  """Find an already-imported module by its filesystem path.

  Args:
    path: The filesystem path to search for in sys.modules.

  Returns:
    A tuple (modname, module) if a module with the given absolute path is found
    in sys.modules, or None if no such module exists.
  """
  apath = os.path.abspath(path)

  for modname, module in sys.modules.items():
    mpath = module_file(module)
    if mpath is not None and mpath == apath:
      return modname, module

install_module(modname, module)

Install a module into sys.modules under the given name.

If a module with the same name already exists in sys.modules, verifies that both modules refer to the same file. Otherwise installs the new module.

Parameters:

Name Type Description Default
modname

The name to register the module under in sys.modules.

required
module

The module object to install.

required

Raises:

Type Description
AssertionError

If a module with the same name already exists at a different file path.

Source code in py_misc_utils/module_utils.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def install_module(modname, module):
  """Install a module into sys.modules under the given name.

  If a module with the same name already exists in sys.modules, verifies that
  both modules refer to the same file. Otherwise installs the new module.

  Args:
    modname: The name to register the module under in sys.modules.
    module: The module object to install.

  Raises:
    AssertionError: If a module with the same name already exists at a
        different file path.
  """
  xmodule = sys.modules.get(modname)
  if xmodule is not None:
    modfile, xmodfile = module_file(module), module_file(xmodule)
    tas.check_eq(modfile,
                 xmodfile,
                 msg=f'Module \"{modname}\" already defined at \"{xmodfile}\"')
  else:
    alog.debug(
        f'Installing module at \"{module_file(module)}\" with \"{modname}\" name')
    sys.modules[modname] = module

load_module(path, modname=None, install=None, add_syspath=None)

Load a Python module from a filesystem path.

If the module is already imported (found via find_module), returns the existing module. Otherwise loads it from disk using importlib, optionally installing it into sys.modules and adding its directory to sys.path.

Parameters:

Name Type Description Default
path

Filesystem path to the .py module file.

required
modname

Optional name to register the module under. If None, derived from the filename. Defaults to None.

None
install

Whether to install the module into sys.modules. Defaults to True.

None
add_syspath

Whether to add the module's directory to sys.path. Defaults to False.

None

Returns:

Type Description

The loaded module object.

Source code in py_misc_utils/module_utils.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def load_module(path, modname=None, install=None, add_syspath=None):
  """Load a Python module from a filesystem path.

  If the module is already imported (found via find_module), returns the
  existing module. Otherwise loads it from disk using importlib, optionally
  installing it into sys.modules and adding its directory to sys.path.

  Args:
    path: Filesystem path to the .py module file.
    modname: Optional name to register the module under. If None, derived from
      the filename. Defaults to None.
    install: Whether to install the module into sys.modules. Defaults to True.
    add_syspath: Whether to add the module's directory to sys.path. Defaults to
      False.

  Returns:
    The loaded module object.
  """
  install = install or True
  add_syspath = add_syspath or False

  name_and_module = find_module(path)
  if name_and_module is not None:
    fmodname, module = name_and_module
    alog.debug(f'Found existing module \"{fmodname}\" for \"{path}\"')
    if install and modname is not None and modname != fmodname:
      install_module(modname, module)

    return module

  apath = os.path.abspath(path)

  parent = find_module_parent(apath) if os.path.basename(apath) != _PYINIT else None
  if parent is not None:
    init_path, mod_path = parent

    parent_module = load_module(init_path, install=True, add_syspath=add_syspath)

    for i in range(len(mod_path) - 1):
      partial = mod_path[:i + 1]
      ipath = os.path.join(os.path.dirname(parent_module.__file__), *partial, _PYINIT)
      if os.path.isfile(ipath):
        imodname = parent_module.__name__ + '.' + '.'.join(partial)
        load_module(ipath, modname=imodname, install=True)

    imodname = parent_module.__name__ + '.' + '.'.join(mod_path)
    alog.debug(f'Importing sub-module \"{imodname}\"')
    module = importlib.import_module(imodname)
  else:
    pathdir = syspath = os.path.dirname(apath)

    if modname is None:
      modname = fsu.drop_ext(os.path.basename(apath), '.py')
      if modname == '__init__':
        syspath, modname = os.path.split(pathdir)

    alog.debug(
        f'Installing module \"{apath}\" with name \"{modname}\" (syspath is \"{syspath}\")'
    )

    if add_syspath:
      add_sys_path(syspath)

    modspec = importlib.util.spec_from_file_location(
        modname, apath, submodule_search_locations=[pathdir])
    module = importlib.util.module_from_spec(modspec)

    if install:
      install_module(modname, module)

    modspec.loader.exec_module(module)

  return module

import_module(name_or_path, modname=None, install=None, add_syspath=None, package=None)

Import a module by name or filesystem path.

If name_or_path is a file path, loads it via load_module. Otherwise imports it as a top-level module using importlib.import_module, optionally installing it under an alternate name.

Parameters:

Name Type Description Default
name_or_path

Module name string or filesystem path to a .py file.

required
modname

Optional alternate name to install the module under in sys.modules. Defaults to None.

None
install

Whether to install the module into sys.modules. Defaults to None (treated as True when modname is specified).

None
add_syspath

Whether to add the module's directory to sys.path (only relevant for file paths). Defaults to None.

None
package

Optional package name to resolve relative imports against. Defaults to None.

None

Returns:

Type Description

The imported module object.

Source code in py_misc_utils/module_utils.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
def import_module(name_or_path,
                  modname=None,
                  install=None,
                  add_syspath=None,
                  package=None):
  """Import a module by name or filesystem path.

  If name_or_path is a file path, loads it via load_module. Otherwise imports
  it as a top-level module using importlib.import_module, optionally installing
  it under an alternate name.

  Args:
    name_or_path: Module name string or filesystem path to a .py file.
    modname: Optional alternate name to install the module under in sys.modules.
      Defaults to None.
    install: Whether to install the module into sys.modules. Defaults to None
      (treated as True when modname is specified).
    add_syspath: Whether to add the module's directory to sys.path (only
      relevant for file paths). Defaults to None.
    package: Optional package name to resolve relative imports against.
      Defaults to None.

  Returns:
    The imported module object.
  """
  if os.path.isfile(name_or_path):
    module = load_module(name_or_path,
                         modname=modname,
                         install=install,
                         add_syspath=add_syspath)
  else:
    alog.debug(f'Loading module \"{name_or_path}')
    module = importlib.import_module(name_or_path, package=package)

    if modname is not None and install in (True, None):
      install_module(modname, module)

  return module

module_getter(dot_path)

Create a callable that resolves a dotted path on any object.

Each path element is looked up as an attribute of the previous one; module names encountered on the way are imported (and any submodule path is imported relative to the parent module).

Parameters:

Name Type Description Default
dot_path

Dot-separated attribute path string (e.g. 'a.b.c').

required

Returns:

Type Description

A functools.partial callable that, when called with an object, resolves

dot_path on that object using _module_getter.

Source code in py_misc_utils/module_utils.py
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
def module_getter(dot_path):
  """Create a callable that resolves a dotted path on any object.

  Each path element is looked up as an attribute of the previous one;
  module names encountered on the way are imported (and any submodule
  path is imported relative to the parent module).

  Args:
    dot_path: Dot-separated attribute path string (e.g. 'a.b.c').

  Returns:
    A functools.partial callable that, when called with an object, resolves
    dot_path on that object using _module_getter.
  """
  return functools.partial(_module_getter, dot_path)

import_module_names(modname, names=None)

Import specific names from a module and return them as a tuple.

If names is None, derives a single name from the part of modname after the last dot and imports that attribute from the parent module.

Parameters:

Name Type Description Default
modname

The module name to import from. May contain a dot to separate the module from the attribute name.

required
names

Optional list or single name of attributes to import. If None, the name is derived from modname. Defaults to None.

None

Returns:

Type Description

A tuple of the imported attribute objects from the module.

Source code in py_misc_utils/module_utils.py
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
def import_module_names(modname, names=None):
  """Import specific names from a module and return them as a tuple.

  If names is None, derives a single name from the part of modname after the
  last dot and imports that attribute from the parent module.

  Args:
    modname: The module name to import from. May contain a dot to separate
      the module from the attribute name.
    names: Optional list or single name of attributes to import. If None, the
      name is derived from modname. Defaults to None.

  Returns:
    A tuple of the imported attribute objects from the module.
  """
  if names is None:
    npos = modname.find('.')
    tas.check_gt(npos, 0)
    names = [modname[npos + 1:]]
    modname = modname[:npos]
  else:
    names = ut.expand_strings(names)

  module = importlib.import_module(modname)

  return tuple(module_getter(name)(module) for name in names)

rel_import_module(path, ppath, modname=None, install=None, add_syspath=None)

Import a module relative to another module's file path.

Constructs an absolute path by joining the directory of ppath with '{path}.py' and imports it.

Parameters:

Name Type Description Default
path

The module name (without .py) relative to ppath's directory.

required
ppath

The filesystem path of the reference module.

required
modname

Optional name to register the module under. Defaults to None.

None
install

Whether to install into sys.modules. Defaults to None.

None
add_syspath

Whether to add the module's directory to sys.path. Defaults to None.

None

Returns:

Type Description

The imported module object.

Source code in py_misc_utils/module_utils.py
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
def rel_import_module(path, ppath, modname=None, install=None, add_syspath=None):
  """Import a module relative to another module's file path.

  Constructs an absolute path by joining the directory of ppath with
  '{path}.py' and imports it.

  Args:
    path: The module name (without .py) relative to ppath's directory.
    ppath: The filesystem path of the reference module.
    modname: Optional name to register the module under. Defaults to None.
    install: Whether to install into sys.modules. Defaults to None.
    add_syspath: Whether to add the module's directory to sys.path.
      Defaults to None.

  Returns:
    The imported module object.
  """
  ipath = os.path.abspath(os.path.join(os.path.dirname(ppath), f'{path}.py'))

  return import_module(ipath, modname=modname, install=install, add_syspath=add_syspath)

maybe_import(modname, package=None)

Attempt to import a module, returning None on failure.

Logs a spam-level message if the import fails rather than raising.

Parameters:

Name Type Description Default
modname

The module name to import.

required
package

Optional package to resolve relative imports against. Defaults to None.

None

Returns:

Type Description

The imported module object, or None if the import fails.

Source code in py_misc_utils/module_utils.py
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
def maybe_import(modname, package=None):
  """Attempt to import a module, returning None on failure.

  Logs a spam-level message if the import fails rather than raising.

  Args:
    modname: The module name to import.
    package: Optional package to resolve relative imports against.
      Defaults to None.

  Returns:
    The imported module object, or None if the import fails.
  """
  try:
    return importlib.import_module(modname, package=package)
  # Optional import: module bodies run arbitrary code and can fail in any
  # way - a broken optional dependency is logged and reported as missing.
  except Exception:  # noqa: BLE001
    alog.spam(f'Unable to load module \"{modname}\" in package \"{package}\"')

module_file(module)

Return the file attribute of a module, or None if not present.

Parameters:

Name Type Description Default
module

A module object.

required

Returns:

Type Description

The file string attribute of the module, or None if the module has no

file attribute.

Source code in py_misc_utils/module_utils.py
372
373
374
375
376
377
378
379
380
381
382
def module_file(module):
  """Return the __file__ attribute of a module, or None if not present.

  Args:
    module: A module object.

  Returns:
    The __file__ string attribute of the module, or None if the module has no
    __file__ attribute.
  """
  return getattr(module, '__file__', None)

clone_module(modname)

Clone (re-import) a module by name, returning a fresh module object.

Finds the module spec, creates a new module from it, and executes the loader to populate it. This produces a module independent of any cached version in sys.modules - a fresh copy of the module body, with its own state, sharing nothing with the original module object.

Parameters:

Name Type Description Default
modname

The name of the module to clone.

required

Returns:

Type Description

A newly loaded module object for modname.

Source code in py_misc_utils/module_utils.py
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
def clone_module(modname):
  """Clone (re-import) a module by name, returning a fresh module object.

  Finds the module spec, creates a new module from it, and executes the
  loader to populate it. This produces a module independent of any cached
  version in sys.modules - a fresh copy of the module body, with its own
  state, sharing nothing with the original module object.

  Args:
    modname: The name of the module to clone.

  Returns:
    A newly loaded module object for modname.
  """
  specs = importlib.util.find_spec(modname)
  module = importlib.util.module_from_spec(specs)
  specs.loader.exec_module(module)

  return module