Python Forum
Python3x, from_import(name, from) function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3x, from_import(name, from) function
#7
Several months ago I had a similar problem - dynamic import in Python3. This is my solution - luckily, I've preserved it

def dynamic_import(module_name, path2module):
    """
    Dynamic import of module
    :param module_name: name of module
    :param path2module: path to module
    :return: module object
    """
    dynamic_spec = imp_util.spec_from_file_location(
        module_name, '{}/{}.py'.format(path2module, module_name))
    dynamic_module = imp_util.module_from_spec(dynamic_spec)
    dynamic_spec.loader.exec_module(dynamic_module)
    return dynamic_module
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


Messages In This Thread
RE: Python3x, from_import(name, from) function - by volcano63 - May-13-2017, 09:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3x running only with one instance (how can I prevent too many running instance) harun2525 5 21,064 Jul-21-2017, 07:36 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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