Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import class dynamically
#1
I have two base classes FirstBaseClass and SecondBaseClass and lot of theirs subclasses. Each subclass defined in the separate file, location of these files is known. I need to import and instantiate subclasses programmatically. Now I use following approach:

import inspect
import importlib

class_name = "FirstChildClass"
file_path = "/path/to/class.py"

try:
    spec = importlib.util.spec_from_file_location(class_name, file_path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    for x in dir(module):
        obj = getattr(module, x)
        if inspect.isclass(obj) and (issubclass(obj, FirstBaseClass) or issubclass(obj, SecondBaseClass)) and obj.__name__ == class_name:
            return obj()
except ImportError as e:
    print(e)    
But problem is that I need to know class name. Is it possible to import class from file without knowing its name, assuming that only one class defined in that file?
Reply


Messages In This Thread
Import class dynamically - by voltron - Jan-30-2018, 06:42 AM
RE: Import class dynamically - by Gribouillis - Jan-30-2018, 07:04 AM
RE: Import class dynamically - by voltron - Jan-30-2018, 08:44 AM
RE: Import class dynamically - by Gribouillis - Jan-30-2018, 09:05 AM
RE: Import class dynamically - by voltron - Feb-05-2018, 01:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Dynamically setting nested class be_ams 0 1,586 Apr-06-2022, 02:09 PM
Last Post: be_ams
  dynamically define class's michavardy 1 2,180 Feb-26-2019, 04:20 PM
Last Post: buran
  Help creating a class instance dynamically Kotevski 9 5,576 Aug-17-2018, 05:23 AM
Last Post: Gribouillis
  import just one class from a file sylas 4 3,404 Apr-25-2018, 08:56 PM
Last Post: sylas

Forum Jump:

User Panel Messages

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