importing module through variable

Michael Hudson mwh21 at cam.ac.uk
Tue Jun 13 11:26:05 EDT 2000


=?iso-8859-1?Q?Andr=E9_Dahlqvist?= <andre at beta.telenordia.se> writes:

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

getattr(the_module,var)

Python is gloriously regular in some ways...


> Or is there some equivalence to "from the_module import
> the_dictionary" when you're using __import__?

Probably.  But you want the above, if I understand you properly...

Cheers,
M.

-- 
41. Some programming languages manage to absorb change, but 
    withstand progress.
  -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html



More information about the Python-list mailing list