Finding and loading subclasses dynamically. issubclass(x, base_plugin.Plugin) fails.

Arnaud Delobelle arnodel at googlemail.com
Sun Aug 29 06:40:28 EDT 2010


Osmo Maatta <osmoma at gmail.com> writes:

> Hello,
>
> Sub class test fails.
> ======================
> I have a program that needs to load plugin-classes during runtime.
>
> The program has these subdirectories (modules).
>
> $ tree
> .
> `-- test.py
> |	
> |-- plugins
> |   |-- base_plugin.py
> |   |-- base_plugin.pyc
> |   |-- __init__.py
> |   `-- oca
> |       |-- __init__.py
> |       |-- open_clipart.py
>
>
> The plugins (sub directory) contains one or more plugin modules, in this
> test-case there is only one; oca/open_clipart.py.
>
> The plugins/base_plugin.py (contains class Plugin()) is a base class of
> all plugins.
>
> I want to list and load all plugin-classes (that inherit from
> base_plugin.Plugin). I have a Python code that successfully browses the
> plugins, but the the test issubclass(x, base_plugin.Plugin) fails.
>
> Please see this Python code:
> http://futuredesktop.com/tmp/test6.tar.gz
>
> Why the issubclass(entry, cls) test fails?
>
> if issubclass(entry, cls):
> ....print "Found a subclass: " + key
> ....subclasses.append(entry)
>
> I can see that the class names are right, even objects of these classes
> are ok.
>
> My OS is Ubuntu 10.04.
> $ python --version
> Python 2.6.5
>
> The actual, final product will be this
> http://www.futuredesktop.com/clipart-applet/clipart-applet.ogv
> It is a ClipArt query-engine and browser.
> It can translate queries from "any" language to english ;-)
>
> Most kindly
>   Osmo Antero Maatta
>   Grønland, Oslo

Hi,

I haven't looked closely at your problem, but are you aware of the
__subclasses__ class method?  E.g.

>>> class A(object): pass
... 
>>> class B(A): pass
... 
>>> class C(A): pass
... 
>>> A.__subclasses__()
[<class '__main__.B'>, <class '__main__.C'>]
>>> 

This might help you.

-- 
Arnaud



More information about the Python-list mailing list