python module loading

Started by
2 comments, last by CoMaNdore 18 years, 2 months ago
note: I re wrote this post to make it easier to answer. Okay after seaching around the net for a while I tought I had found the golden egg. But it turns out that it wont work the way I want it to work. Problem: I want to load a module based on a absulute path ( or relative to the module ). But I cant.. Say I have a module named loader.py in "test"'s directory. inside test there is also a folder named "data". Inside data there is 2 files named "loadme.py" and "importme.py"

data/
     loader.py
     
     data/
          loadme.py
          importme.py
Now as I said I thought had found the golden egg: ModulesAsPlugins - http://wiki.python.org/moin/ModulesAsPlugins The problem is that each time I run the example found in the above article like this: (and belov)

# in loader.py
modules = [load_module(name) for name in find_modules("data")];

# I get this error:
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in ?
    modules = [load_module(name) for name in find_modules("data")];
  File "C:\Coding\MyProjects\MazeRunner\loadtest\loader.py", line 35, in load_module
    (file, pathname, desc) = imp.find_module( name, path );
ImportError: No module named loadme
Can anybody point out for me what I am doing wrong? For those of you that are laszy I copied my code:

#
# Loader
#


import os
import sets
import imp

def find_modules( path="." ):
    """ Return names of modules in a directory """


    modules = sets.Set();
    for filename in os.listdir(path):
        module = None;

        if filename.endswith( ".py" ):
            module = filename[:-3];
        elif filename.endswith( ".pyc" ):
            module = filename[:-4];


        if module != None:
            modules.add( module );


    return list(modules);



def load_module( name, path=["."] ):
    """Return a named module found in a given path."""

    (file, pathname, desc) = imp.find_module( name, path );
    return( imp.load_module(name, file, pathname, desc ) );


modules = [load_module(name) for name in find_modules("data")];

[Edited by - CoMaNdore on January 21, 2006 6:57:36 AM]
- Me
Advertisement
Do you have documentation for the imp module? I don't think it's standard, is it? The error is in that, so the documentation for it would be a good place to start.

Anyway, here's my speculation: notice that you pass a path to find_module, but omit it for load_module. Try this instead:

modules = [load_module(name, "data") for name in find_modules("data")];
Stick a file named '__init__.py' inside the second data directory that imports the two other modules. Then just 'import data; data.loadme.blah()'.

See packages.
Kylotan, your little 'hint' fixed the code... it now works like
a charm. :D

Anwyay some info: the imp module is a standard module.
it is used to access the internal's of the import statement.

And to help out other that may have the same problem that I had here is the
now working complete code:

## modloader.py# #import osimport setsimport impdef LoadModules( path ):    """ Load all modules in @path. Return a dict with modules """    ret = {};    for mod in find_modules( path ):        ret[mod] = load_module(name, [path]);    return( ret );def LoadModule( name, path = "." ):    """ Load a single module spesified by @name and @path """        for mod in find_modules( path ):        if mod == name:            return( load_module(name, [path]) );    return( None );    def find_modules( path="." ):    """ Return names of modules in a directory """    modules = sets.Set();    for filename in os.listdir(path):        module = None;            if filename.endswith( ".py" ):            module = filename[:-3];        elif filename.endswith( ".pyc" ):            module = filename[:-4];        if module != None:            modules.add( module );    return list(modules);def load_module( name, path=["."] ):    """Return a named module found in a given path."""    (file, pathname, desc) = imp.find_module( name, path );    return( imp.load_module(name, file, pathname, desc ) );



Thanks for the help!

[Edited by - CoMaNdore on January 22, 2006 11:32:56 AM]
- Me

This topic is closed to new replies.

Advertisement