Automatic import of submodules

Shambhu Rajak Shambhu.Rajak at kpitcummins.com
Sun Nov 27 23:31:59 EST 2011


-----Original Message-----
From: Massi [mailto:massi_srb at msn.com] 
Sent: 25/11/2011 6:30 PM
To: python-list at python.org
Subject: Automatic import of submodules

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.


=====
What you could do is : 

Instead of importing method wise , import the module.

from plugins.wav_plug import WavPlug  

you can then use like this then
WavPlug.WavPlug

This will avoid the-every time importing of each method

Regards,
Shambhu





More information about the Python-list mailing list