Importlib#

This is a package that implements the functionality of the import keyword. It can be useful in some cases when you need to do some tricky import. See the appropriate section of the offical documentation for more information.

import sys
import importlib
from pathlib import Path

sys.path.append("/tmp")

Import module#

This section provides an overview of the options for loading modules with importlib. The central option here is the importlib.import_module function. You just have to specify the name of the module as it is typically written in the import keyword.


The following cell imports cowsay using importlib.import_module.

cowsay_object = importlib.import_module("cowsay")
cowsay_object
<module 'cowsay' from '/home/fedor/.virtualenvs/python/lib/python3.12/site-packages/cowsay/__init__.py'>

It returns module object - the objects that act just like a regular module loaded with the import keyword.

The following cells clearly prove that cowsay_object is just a regular object.

print(cowsay_object.cowsay("import module"))
 _______________ 
< import module >
 --------------- 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
print(cowsay_object.cowthink("it's really module"))
 ____________________ 
( it's really module )
 -------------------- 
        o   ^__^
         o  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

import_module arguments#

Description of the importlib.import_module(name, package) function in the official documentation can be a bit complicated.

The name argument specifies where to look for a module relative to the package, where package is just a folder.


The following cell creates a file structure just as shown in the example in the documentation.

import os
os.chdir("/tmp")
(Path("pkg")/"subpkg").mkdir(parents=True, exist_ok=True)
%%writefile pkg/mod.py
def hello_mod():
    print("hello from mod")
Writing pkg/mod.py

So there is a “package” pkg.subpkg and at the same level there is a module mod.py

!tree pkg
pkg
├── mod.py
└── subpkg

2 directories, 1 file

The following cell executes importlib.import_module as it’s shown in the official documentation. And proves that import really works.

mod = importlib.import_module("..mod", "pkg.subpkg")
mod.hello_mod()
hello from mod

From optional file#

You can also load the module from the optional file. To do this, you need to:

  • Get spec using importlib.util.spec_from_file_localtion.

  • Get module from spec with importlib.util.module_from_spec.

  • Execute module with spec’s exec_module method.


The following cell creates a python module in the optional location.

%%writefile /tmp/some_module.py
def fun():
    print("some input")
Writing /tmp/some_module.py

The following cell imports this module and shows that it works expected.

spec = importlib.util.spec_from_file_location(
    "some_module", "/tmp/some_module.py"
)
some_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(some_module)
some_module.fun()
some input

Reload#

Python only executes the module’s code the first time the module is imported. importlib.reload allows the module to be executed again.


The following cell creates a python module that we’ll use as an example.

%%writefile /tmp/my_module.py
print("my module is imported")
Overwriting /tmp/my_module.py

The next cell imports the module.

import my_module
my module is imported

As a result, the module was executed and the message specified in the module is printed.

The following cell attempts to reload the module.

import my_module

As a result, there is no message again - because python didn’t execute the module again. But the following code uses the importlib.reload function.

importlib.reload(my_module)
my module is imported
<module 'my_module' from '/tmp/my_module.py'>

Note. The function returned the module object, but you don’t have to reassign the module to it’s name - it will be done automatically.