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]