importing module through variable

Steve Tregidgo smst at bigfoot.com
Tue Jun 13 11:31:27 EDT 2000


Hi André,

André Dahlqvist wrote:
> 
> > You want the __import__ function:
> >
> > module = __import__(var)
> 
> I misphrased the question. Inside my module I have a dictionary that I
> need to access. And the name of this dictionary is stored in a
> variable. If I do:
> 
> import the_module
> 
> How can I then access the dictionary inside this module, if the name
> of the dictionary is stored in a variable? Or is there some
> equivalence to "from the_module import the_dictionary" when you're
> using __import__?

How about getattr?  Like this:

    import my_module
    the_dict = getattr(my_module, 'dict_name')

This should give you the same result as:

    import my_module
    the_dict = my_module.dict_name

HTH,
Steve

-- 
Steve Tregidgo
Software Developer
http://www.businesscollaborator.com




More information about the Python-list mailing list