Python 3 dynamic import follies - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python 3 dynamic import follies (/thread-3359.html) |
Python 3 dynamic import follies - volcano63 - May-17-2017 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 . 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 , unhelpful helps , 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 |