I'm writing a python applet for data acquisition from scientific instruments and I'm trying to figure out the best way to manage my device "drivers".
Each driver is represented by a separate python module in the package that is my program. They each implement some standard interface, but mostly it's a gigantic list of commands (and function mappings) that are specific to each device and bus involved. In the long run, (I'm writing this for my lab group and am planning on supporting a few dozen or so devices) I want to avoid loading all of them in at once. Instead, at run-time, I want to read in a list of modules into a dictionary/list and then load them only when they are actually needed.
When the user wants to use a new device, he selects the driver to use, and passes the name along to the driver subsystem which then checks to see if that driver is in the list of loaded modules, and if it's not, it calls the __import__
function and loads the driver in then instantiates a device object using the driver and hands it back to the user to use.
My question is: What is the best way to get a list of all modules in a relative way? What I mean is, if I know that the drivers are located in ..drivers
is there a way to get a tidy list of modules in that subpacakage? To illustrate: usually I just call from ..drivers import driver_name
to import the module, but since I'm not guaranteed to be in the package directory, can't just us os
to get a list of module names.
In any case, any ideas (even maybe a better way to accomplish what I want - loadable "drivers") would be appreciated.