I am trying to load a class at run time from a configuration file. The module that contains the class will contain many other classes and I don't want to import them all. The pattern given in this question Import class from module dynamically
cls = getattr(import_module('my_module'), 'my_class')
loads in the entire module which is exactly what I am trying to avoid. Is there way to get just 'my_class' without everything else in 'my_module'?
from my_module import my_class. Technically all the code in the module will still be executed, but onlymy_classwill be visible in the importing module. Re 2: They will also presumably be able to change your existing classes. Maybe them being able to change this is the root of the problem. N.B. If two objects (e.g. classes) are bound to the same name, only the second definition "survives".my_class = getattr(__import__("my_module"), "my_class")?