Re: Importing module with name given as a variable



On Sat, 30 Aug 2008 11:02:03 -0700 (PDT), Sean Davis wrote:
What is the "best practice" for importing an arbitrary module given
that the name is stored in a variable? The context is a simple web
application with URL dispatching to a module and function. I know of
__import__(), the imp module, and exec. For each of these, is there a
way to make them work "just like" the normal import call?

It all depends on your needs. If you have to deal with user
input to get the name of the module, better use __import__
than exec. __import__ works just like the normal import call:

import sys

is equal to:

sys = __import__('sys')

If you need, let's say, to search for a module that lies
outside sys.module paths, then use imp's functions.

HTH.

--
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/
.



Relevant Pages

  • Re: The lonely American
    ... context and misrepresenting them. ... pages again for new lies to spew. ... killed in a terrorist assassination plot; a remark she was joking about ...
    (misc.survivalism)
  • Re: HotGhettoMess .com Website Says Bill Cosby Is Right-On
    ... try asking relevant questions instead of those that assume ... ridiculous premises or based on your lies. ... Yer dodging again, instead of facing up to reality. ... >>that you use them out of context is the only thing necessary here. ...
    (soc.culture.african.american)
  • Re: Even blogger, Yoani Sanchez, opposes US embargo -- calls it "clumsy"
    ... I expose your lies about what the true meaning of the words you misquoted. ... As usual you take snippets out of context and claim that they mean something completely different than the author meant. ... consumed in Cuba, nore than the 20% produced locally you mean. ... agents" or "lobbyists" to the worst things one can imagine) and direct ...
    (soc.culture.cuba)
  • Re: Dan Christensen: reduced to utter despair after been exposed as a liar over and over again
    ... What desperate Dan snipped in despair as he doesn't want people to get to the context. ... A pity for him is that even his selected snippets expose his lies. ... For those that want to see the full thread and the complete messages in their context, here is a link to the threads concerned. ...
    (soc.culture.cuba)
  • Re: A menace?
    ... We're back to your pathetic repetition of lies and gross distortions. ... None of them are in the context of their threads. ... to ridicule for all your lies, libels and downright ignorance. ... You are quoting from someone who has been kicked off both the forums you ...
    (uk.environment.conservation)