myparentclass.__subclasses__() not working for me

Discussion in 'Python' started by samwyse, Sep 14, 2009.

  1. samwyse

    samwyse Guest

    ### I've tried this under both Python 2.5.1 and 3.1.1, and it isn't
    working with either one. Here is my program:

    class Plugin(object):
    """This is the base object for a plug-in."""
    pass

    def load_plugins(plugin_subdir='plugins'):
    import sys, pkgutil, imp, os.path

    try:
    # Use this path if we're running as a module.
    homedir = __path__[0]
    except NameError:
    # Use this path if we're running stand-alone.
    homedir = sys.path[0]
    plugin_path = [ os.path.join(homedir, plugin_subdir) ]

    modules = {}
    for loader, name, is_pkg in pkgutil.iter_modules(plugin_path):
    file, pathname, desc = imp.find_module(name, plugin_path)
    modules[name] = imp.load_module(name, file, pathname, desc)
    for pair in modules.items():
    print('name = %r\nmodule = %r\n' % pair)

    if __name__ == '__main__':
    print('subclasses = %r\n' %(Plugin.__subclasses__()))
    load_plugins()
    print('subclasses = %r\n' %(Plugin.__subclasses__()))

    ### And here is my plugin, in plugins/myplugin.py:

    from plugin import Plugin
    class MyPlugin(Plugin):
    pass

    ### When I run the main program, I get this:

    subclasses = []

    name = 'myplugin'
    module = <module 'myplugin' from 'C:\Documents and Settings\sam_denton
    \Desktop\scripting\plugins\myplugin.py'>

    subclasses = []

    ### Obviously, myplugin is being found found and loaded, but my base
    class doesn't know about it. Any ideas?
    samwyse, Sep 14, 2009
    #1
    1. Advertising

  2. En Mon, 14 Sep 2009 10:30:07 -0300, samwyse <> escribió:

    > ### I've tried this under both Python 2.5.1 and 3.1.1, and it isn't
    > working with either one. Here is my program:
    >
    > class Plugin(object):
    > """This is the base object for a plug-in."""
    > pass
    >
    > def load_plugins(plugin_subdir='plugins'):
    > import sys, pkgutil, imp, os.path
    >
    > try:
    > # Use this path if we're running as a module.
    > homedir = __path__[0]
    > except NameError:
    > # Use this path if we're running stand-alone.
    > homedir = sys.path[0]
    > plugin_path = [ os.path.join(homedir, plugin_subdir) ]
    >
    > modules = {}
    > for loader, name, is_pkg in pkgutil.iter_modules(plugin_path):
    > file, pathname, desc = imp.find_module(name, plugin_path)
    > modules[name] = imp.load_module(name, file, pathname, desc)
    > for pair in modules.items():
    > print('name = %r\nmodule = %r\n' % pair)
    >
    > if __name__ == '__main__':
    > print('subclasses = %r\n' %(Plugin.__subclasses__()))
    > load_plugins()
    > print('subclasses = %r\n' %(Plugin.__subclasses__()))
    >
    > ### And here is my plugin, in plugins/myplugin.py:
    >
    > from plugin import Plugin
    > class MyPlugin(Plugin):
    > pass
    >
    > ### When I run the main program, I get this:
    >
    > subclasses = []
    >
    > name = 'myplugin'
    > module = <module 'myplugin' from 'C:\Documents and Settings\sam_denton
    > \Desktop\scripting\plugins\myplugin.py'>
    >
    > subclasses = []
    >
    > ### Obviously, myplugin is being found found and loaded, but my base
    > class doesn't know about it. Any ideas?


    Don't run the first module as a program itself. When it's run as a
    program, it's known as '__main__'; when someone executes 'import plugin'
    *another* copy is imported under the name 'plugin'.
    So your plugin classes inherit from plugin.Plugin but you're printing
    __main__.Plugin subclasses.

    --
    Gabriel Genellina
    Gabriel Genellina, Sep 14, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ratman
    Replies:
    0
    Views:
    643
    Ratman
    Sep 14, 2004
  2. Martin Heuckeroth
    Replies:
    5
    Views:
    667
    JiangZemin
    Apr 1, 2005
  3. Alan Silver
    Replies:
    1
    Views:
    3,752
    Alan Silver
    Aug 2, 2005
  4. gaurav tyagi
    Replies:
    14
    Views:
    1,304
    gaurav tyagi
    Jan 20, 2006
  5. Ruud de Jong
    Replies:
    4
    Views:
    322
    Terry Reedy
    Feb 28, 2004
Loading...

Share This Page