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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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 | |