J
John Allman
Hi all,
I'm trying to create a setup which allows a program to request an object
using strings and get an object of that type. It appears to be mostly
working but i have difficulties if i attempt to load a module at runtime
and then request an object of that type.
Essentially, my code works as follows:
I have a registry class which has a method to register a class. It
inserts an entry into a dictionary using a tuple of two strings (read
from attributes of the class) that points to the class. It also has a
method which will create an object of that class type which takes two
strings as an argument.
Every class i want to use this technique inherits from a class which
uses a custom metaclass. The custom metaclass' __init__ function
registers them with the registry class.
The registry class has another method which allows you to specify a path
to place your plugins in. In reality it merely appends this string to
sys.path. The registry "create" function has a little extra smarts in
that if it can't find the class already in the dictionary, it will
attempt to load the appropriate module. It does this by using __import__
and uses the two strings supplied with a dot in between as the module name.
If i manually import a module, this method works a treat, however if i
attempt to dynamically load a module at runtime the create method fails
with the following error:
TypeError: 'NoneType' object is not callable
I can see that the module is getting imported as i have set print
statements in the custom metaclass to let me know when it is registering
a new class. I can only conclude then that there is some difference
between using the import statement and the __import__ function.
I don't want to clutter the mailing list so i haven't attached any code,
but i certainly can supply it assuming the answer isn't something
obvious i've missed. Am i going about this completely the wrong way? Is
there already an established approach for an auto-registering,
dynamically loading plugin setup? Any help greatly appreciated.
I'm not subscribed to the list, so please cc me in any replies.
Thanks,
John Allman
I'm trying to create a setup which allows a program to request an object
using strings and get an object of that type. It appears to be mostly
working but i have difficulties if i attempt to load a module at runtime
and then request an object of that type.
Essentially, my code works as follows:
I have a registry class which has a method to register a class. It
inserts an entry into a dictionary using a tuple of two strings (read
from attributes of the class) that points to the class. It also has a
method which will create an object of that class type which takes two
strings as an argument.
Every class i want to use this technique inherits from a class which
uses a custom metaclass. The custom metaclass' __init__ function
registers them with the registry class.
The registry class has another method which allows you to specify a path
to place your plugins in. In reality it merely appends this string to
sys.path. The registry "create" function has a little extra smarts in
that if it can't find the class already in the dictionary, it will
attempt to load the appropriate module. It does this by using __import__
and uses the two strings supplied with a dot in between as the module name.
If i manually import a module, this method works a treat, however if i
attempt to dynamically load a module at runtime the create method fails
with the following error:
TypeError: 'NoneType' object is not callable
I can see that the module is getting imported as i have set print
statements in the custom metaclass to let me know when it is registering
a new class. I can only conclude then that there is some difference
between using the import statement and the __import__ function.
I don't want to clutter the mailing list so i haven't attached any code,
but i certainly can supply it assuming the answer isn't something
obvious i've missed. Am i going about this completely the wrong way? Is
there already an established approach for an auto-registering,
dynamically loading plugin setup? Any help greatly appreciated.
I'm not subscribed to the list, so please cc me in any replies.
Thanks,
John Allman