Dynamical loading of modules

Carsten Haese carsten at uniqsys.com
Tue Oct 4 09:00:10 EDT 2005


On Tue, 2005-10-04 at 08:32, Steve Holden wrote:
> Carsten Haese wrote:
> > On Mon, 2005-10-03 at 17:37, Steve Holden wrote:
> > 
> >>Carsten Haese wrote:
> >>
> >>>On Mon, 2005-10-03 at 16:41, Carsten Haese wrote:
> >>>
> >>>
> >>>>On Mon, 2005-10-03 at 15:52, Jacob Kroon wrote:
> >>>>
> >>>>
> >>>>>Hi, I'm having some problems with implementing dynamical module loading. 
> >>>>>First let me
> >>>>>describe the scenario with an example:
> >>>>>
> >>>>>modules/
> >>>>>   fruit/
> >>>>>       __init__.py
> >>>>>       apple.py
> >>>>>       banana.py
> >>>>>
> >>>>>apple.py defines a class 'Apple', banana defines a class 'Banana'. The 
> >>>>>problem lies in the
> >>>>>fact that I want to be able to just drop a new .py-file, for instance 
> >>>>>peach.py, and not change
> >>>>>__init__.py, and it should automatically pickup the new file in 
> >>>>>__init__.py. I've come halfway
> >>>>>by using some imp module magic in __init__.py, but the problem I have is 
> >>>>>that the instantiated
> >>>>>objects class-names becomes fruit.apple.Apple/fruit.banana.Banana, whild 
> >>>>>I want it to be
> >>>>>fruit.Apple/fruit.Banana.
> >>>>>
> >>>>>Is there a smarter way of accomplishing what I am trying to do ?
> >>>>>If someone could give me a small example of how to achieve this I would 
> >>>>>be very grateful.
> >>>>
> >>>>How about something like this in fruit/__init__.py:
> >>>>
> >>>>import os
> >>>>
> >>>>fruit_dir = os.path.dirname(__file__)
> >>>>fruit_files = [x for x in os.listdir(fruit_dir) if (x[-3:]=='.py' and x!='__init__.py')]
> >>>>for fruit_file in fruit_files:
> >>>> module_name = fruit_files[:-3]
> >>>
> >>>                  ^^^^^^^^^^^ This should be fruit_file, of course.
> >>>
> >>>
> >>>
> >>>> exec "from %s import *" % module_name
> >>>>
> >>
> >>Wouldn't
> >>
> >>     __import__(module_name)
> >>
> >>be better.
> > 
> > 
> > I don't see how a working example that meets the OP's requirements can
> > be constructed using __import__, but that may easily be due to my lack
> > of imagination. How would you do it?
> > 
> I was simply suggesting that you replace the exec statement with a call 
> to __import__(). Wouldn't that work?

Not the way I tried it by simply replacing my line with your line. (If
it matters, I'm on python 2.2 here.) First of all, the __import__
variant doesn't see the submodules unless I add fruit_dir to sys.path.
Secondly, the OP's requirements are that the classes that the submodules
implement be imported into fruit's namespace, and I don't see how to
make __import__ do that.

Regards,

Carsten Haese.





More information about the Python-list mailing list