Import if condition is correct

Steve Holden steve at holdenweb.com
Sat Oct 28 07:12:58 EDT 2006


MindClass wrote:
> Georg Brandl wrote:
> 
>>MindClass wrote:
>>
>>>Is possible import a library according to a condition?
>>>
>>>if Foo == True:
>>>    import bar
>>>
>>
>>Why don't you try it?
>>
> 
> I thinked that could be another way for import statement.
> 
> In that case I'll have to set a global variable before of the import
> statements although I'd prefer not use them.
> 

I'm guessing that you think this might be necessary to avoid importing 
the same module multiple times: it's not. Python only runs the module's 
code the first time the module is imported into a program. A further 
import statement effectively does noting, because the interpreter sees 
(from an entry in the sys.modules dictionary) that the module is already 
present.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list