Python dynamic import third party sub-modules by importlib's question

Viewed 32

Python version: 3.8.10

Hi, I am trying to import a third party sub-module using importlib by giving the string name of sub-module since the user input may only has name of the sub-modules, for example, while running the script:

import importlib

pkg = importlib.import_module("transformers")
print(dir(pkg))
print(dir(pkg.BertTokenizer))

mds = importlib.import_module("transformers.BertTokenizer")
print(dir(mds))

It shows that the first pkg is OK to be loaded, yet mds is failed. The output message:

['ALBERT_PRETRAINED_CONFIG_ARCHIVE_MAP',...,'xnli_tasks_num_labels']
['SPECIAL_TOKENS_ATTRIBUTES', '__annotations__',...,'vocab_size']

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    mds = importlib.import_module("transformers.BertTokenizer")
  File "c:\users\ychuang\appdata\local\programs\python\python38\lib\importlib\__init__.py", line 1
27, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'transformers.BertTokenizer'

My question is if there is a corrected way to perform the method like mds = importlib.import_module("transformers.BertTokenizer")?

0 Answers
Related