Re: dynamic module loading via __import__, nonetype?

2006-10-18 Thread John Allman
Gabriel Genellina wrote:
 At Monday 16/10/2006 13:33, John Allman wrote:
 
 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
 
 Can you cut down a reproducible test case, with *only* this behavior and
 nothing more?


Hi Gabriel

Thanks for the reply. I actually spotted my error (simple, stupid that i
hadn't noticed). I had the create method creating an object if it could
find a suitable class and importing a suitable class if it couldn't find
one. I rather stupidly forgot to actually create an object after
importing the class!

I was completely stuck - it's amazing what a night's sleep will do for you!

Thanks,

John
-- 
http://mail.python.org/mailman/listinfo/python-list


dynamic module loading via __import__, nonetype?

2006-10-16 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list