Plugin system; imp module head scratch

Discussion in 'Python' started by Dave, Dec 19, 2004.

  1. Dave

    Dave Guest

    Hi, all,

    I'm trying to implement a simple plugin framework, with some
    unexpected results. I'm using Python 2.3.4 on Windows 2000.

    My script loader.py loads plugin packages from a directory (in this
    case its own), and checks a variable defined in the package's
    __init__.py for information on the plugin.

    The directory in which loader.py sits is itself a package::

    plugin_test
    |
    + Plugin1
    | |
    | __init__.py
    |
    __init__.py
    loader.py

    I must be misunderstanding how the imp module works, or something.
    Although the correct package is returned by my helper method, its
    member variables are those of the the parent package, plugin_test.

    The output of this demo is::

    <module 'Plugin1' from '__init__.pyc'>
    Directory plugin_test

    Which seems contradictory. What have I got wrong?

    Best Regards,

    Dave

    <Code follows>

    This is plugin_test/Plugin1/__init__.py::

    dir = "plugin_test/Plugin1"


    This is plugin_test/__init__.py::

    dir = "plugin_test"


    And here is the loader script, plugin_test/loader.py::

    import os
    import imp

    def GetPackage(name, peerFile, relPath = '.'):
    """
    Return the named Python package on a
    relative path from the file given
    """
    path = os.path.normpath(
    os.path.join(os.path.dirname(peerFile), relPath))
    try:
    fObj, path, descr = imp.find_module(name, [path])
    except ImportError:
    return None
    if descr[2] != imp.PKG_DIRECTORY:
    return None
    try:
    return imp.load_module(name, None, '', descr)
    except:
    return None

    if __name__ == "__main__":
    pkg = GetPackage("Plugin1", __file__, '.')
    print pkg
    print "Directory ", pkg.dir
    Dave, Dec 19, 2004
    #1
    1. Advertising

  2. Dave

    Keith Dart Guest

    Dave wrote:
    > Hi, all,
    >
    > I'm trying to implement a simple plugin framework, with some
    > unexpected results. I'm using Python 2.3.4 on Windows 2000.


    What would be the difference between a "plugin" and a regular Python module?



    -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Keith Dart <>
    public key: ID: F3D288E4
    =====================================================================
    Keith Dart, Dec 19, 2004
    #2
    1. Advertising

  3. Dave

    Nick Coghlan Guest

    Dave wrote:
    > Hi, all,
    >
    > I'm trying to implement a simple plugin framework, with some
    > unexpected results. I'm using Python 2.3.4 on Windows 2000.
    >
    > My script loader.py loads plugin packages from a directory (in this
    > case its own), and checks a variable defined in the package's
    > __init__.py for information on the plugin.


    Packages and modules are really rather different beasts. I wouldn't be surprised
    to find that 'find_module' and 'load_module' behave strangely when used for
    packages.

    Is there any reason you can't temporarily alter sys.path and use __import__
    normally? The behaviour of that is generally less quirky.


    Anyway, I believe your specific problem is this line:
    return imp.load_module(name, None, '', descr)

    Try replacing the empty string in the third argument with the path returned from
    the find_module call. (At the moment, it is pulling in the __init__.py from the
    current directory, and assigning it to the name you requested)

    Cheers,
    Nick.

    --
    Nick Coghlan | | Brisbane, Australia
    ---------------------------------------------------------------
    http://boredomandlaziness.skystorm.net
    Nick Coghlan, Dec 19, 2004
    #3
    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. Brian W
    Replies:
    10
    Views:
    750
    Brian W
    Jul 2, 2003
  2. showme

    PHP and ASP.NET go HEAD to HEAD

    showme, Jul 8, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    708
    Sherif ElMetainy
    Jul 10, 2004
  3. Karl
    Replies:
    0
    Views:
    383
  4. Stéphane Klein
    Replies:
    0
    Views:
    259
    Stéphane Klein
    Jul 5, 2011
  5. Replies:
    1
    Views:
    89
Loading...

Share This Page