[PYTHON] Dynamic loading of modules

The previous method (imp.load_module) I found on the Web was deprecated, so I examined the dynamic loading process of modules in Python 3.4.

file organization

top
  |- main.py
  |- foo
          |- __init__.py
          |- some.py
          |- sample.py
  |- bar
          |- __init__.py
          |- some.py

Old way

imp.load_Find by using module_module()Load using the result of_module()Load the module with.


 At this time, the module name (attribute ``` __name__```) is set to the first argument `` `name``` of ``` load_module```.
 In Python, modules are registered in `` `sys.modules```, but be aware that the module loaded first will be overwritten in the second win.
 Therefore, loading some.py of both foo and bar with the same name some can be unintended.

```python
import sys
import imp

def load(dir_name:str, symbol:str): # -> module
    (file, path, description) = imp.find_module(dir_name + '/' + symbol)
    return imp.load_module(symbol, file, path, description)

m = load('foo', 'some')
m.__name__ # -> 'some'
sys.modules['some'] # -> 'module'

n = load('bar', 'some')
m == n # -> True

New way

impAs a replacement forimportlibuse.

importlib.import_With module, load the module with the relative path as it is.



```python
import importlib

m = importlib.import_module('foo.some') # -> 'module'
m.__name__ # -> 'foo.some'

n = importlib.import_module('bar.some') # -> 'module'
n.__name__ # -> 'bar.some'

When you want to change the module name

Is it possible to set the module name freely, ignoring the relative path name, as in the old method? The official documentation introduces a replacement for imp.load_module: sourcefileloader()Can be used to set the module name as its first argument. The module name (some in this case) is still the winner. You can set a different name, so set it as needed.

import importlib.machinery as imm
datum = imm.SourceFileLoader('some', 'foo/some.py').load_module()

Effect of module name

Impact when using some.py with foo / sample.py and when loading dynamically. When reading sample dynamically, the module name specified by import will be the registered name in sys.modules. If you changed the module name above, it also affects the import here.

import some   # or
import foo.some

Summary

Recommended Posts

Dynamic loading of modules
List of python modules
[Python] Loading multi-level self-made modules
Faster loading of Python images
Memorandum of saving and loading model
[Python] Accelerates loading of time series CSV
Summary of modules and classes in Python-TensorFlow2-