Python Forum

Full Version: Python 3 dynamic import follies
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi folks,

It looks that there's one issue of Python where dynamic seems an anathema - dynamic imports. For some reason, in 3.x it keeps changing all the times  Wall .

Several days ago I published this solution - which worked in 3.5 on Mac, and turned absolutely useless in 3.4.3 - as I've found out today.

So I saw this solution on SO - and it worked in iPython 6.0, and did not work in application.

After getting lost in deprecated over deprecated  Huh , unhelpful helps  Confused , and other joys I came up with this solution
try:
   from importlib import machinery as dynamic_importer
except ImportError:
   from imp import machinery as dynamic_importer

def dynamic_import(module_path: pathlib.PosixPath):
   loader = dynamic_importer.SourceFileLoader(module_path.stem, module_path.as_posix())
   return loader.load_module()
Does anyone know why this API keeps on changing, and what is the purpose of importlib.import_module function - when it does not really import modules?

And why - when all is said and done - dynamic import remain cryptic and user-unfriendly? I tried it first in Python 2.7 ab.3 years ago - and event then it was rather cryptic, though more user friendly  Cry