On 11/25/2011 08:00 AM, Massi wrote:
Hi everyone,
in my project I have the following directory structure:
plugins
|
-- wav_plug
|
-- __init__.py
-- WavPlug.py
-- mp3_plug
|
-- __init__.py
-- Mp3Plug.py
...
-- etc_plug
|
-- __init__.py
-- EtcPlug.py
Every .py file contain a class definition whose name is identical to
to the file name, so in my main script I have to import each submodule
like that:
from plugins.wav_plug.WavPlug import WavPlug
from plugins.wav_plug.Mp3Plug import Mp3Plug
and so on. This is uncomfortable, since when a new plugin is added I
have to import it too. So my question is, is it possible to iterate
through the 'plugins' directory tree in order to automatically import
the submodules contained in each subdirectory?
I googled and found that the pkgutil could help, but it is not clear
how. Any hints?
Thanks in advance.
I think the key to the problem is the __import__() function, which takes
a string and returns a module object. So you make a list of the fully
qualified module names (filenames less the extension), and loop through
that list, Then for each one, you can extract items from the module.
It doesn't make sense to define the class names at your top-level,
though, since you'd not have any code to reference any new plugin if it
has a unique class name. So at some point, you're probably going to have
a list or map of such class objects.
--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list