Python Forum
Python 3 dynamic import follies
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3 dynamic import follies
#1
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dynamic object creation using python gary 7 1,271 Oct-15-2022, 01:35 PM
Last Post: Larz60+
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 6,054 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  Dynamic Printing With Python Terminal MrTim 2 2,919 Jul-06-2021, 04:00 PM
Last Post: MrTim
  Use dynamic variable from parallel running python script Sicksym 0 1,850 May-15-2020, 02:52 PM
Last Post: Sicksym
  Import Module and Method ( dynamic ) ? JohnnyCoffee 0 1,513 Mar-23-2020, 04:23 PM
Last Post: JohnnyCoffee
  wordpress-python-xmlrpc. I can import 1 file. How do I import the entire folder? SamLearnsPython 0 3,270 Jul-05-2018, 06:21 AM
Last Post: SamLearnsPython
  Python dynamic inheritance ? harun2525 1 7,510 Apr-28-2017, 11:55 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020