Frank Millman wrote: > Hi all > > I am writing a business/accounting application. Once a user has logged > in they are presented with a menu. Each menu option has a description > and an associated file name and program name. The file name is the name > of a .py file (impName) and the program name is the name of a class in > that file which I instantiate to run the program (progName). > > When a menu option is selected, I execute the program like this - > imp = __import__(impName) > app = getattr(imp,progName)() > > All the .py files are stored in one directory, which I add to sys.path > at the beginning of the session. It all seems to work fine. > > Now my program directory is getting cluttered, so I want to split it > into sub-directories, one per company (it is a multi-company system). I > can do that, and each of the subdirectories can be added to sys.path, > so it should work as at present. > > However, I want the ability to have duplicate program names stored in > different subdirectories. At the time of selecting the menu option I > know which company is active, so I know which directory I want to run > the program from, but there does not seem to be a way to tell 'import' > to import from a particular directory.
I suggest to use module `imp`. For example: I assume paths like this: app/ importer.py company1/ prog1.py the module in the company1 subdirectory: # prog1 class SomeClass(object): def test(self): return "%s: %s" % (__file__, self.__class__.__name__) and the module with your menu could look like this: # importer.py def get_class(classname, impname, company): fp, pathname, description = imp.find_module(impname, [company]) m = imp.load_module(impname, fp, pathname, description) return getattr(m, classname) obj = get_class("SomeClass", "prog1", "company1")() print obj.test() -- HTH, Rob -- http://mail.python.org/mailman/listinfo/python-list