Automatic import of submodules

alex23 wuwei23 at gmail.com
Fri Nov 25 23:15:47 EST 2011


On Nov 25, 11:00 pm, Massi <massi_... at msn.com> wrote:
> plugins
>     |
>     -- wav_plug
>           |
>           -- __init__.py
>           -- WavPlug.py
>     -- mp3_plug
>           |
>           -- __init__.py
>           -- Mp3Plug.py
> ...
>     -- etc_plug
>           |
>           -- __init__.py
>           -- EtcPlug.py

What do you gain by having each plugin as a package? Unless you're
storing other resources with each plugin, I'd move all your XXXPlug.py
files into plugins/ I'd also probably call the modules 'wavplug' and
the class 'WavPlug' to always make it clear  to which you're
referring.

> 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?

It's not exactly automatic, but you could move all of those imports
into plugins/__init__.py, then just do a single

   from plugins import *

in your main module.



More information about the Python-list mailing list