Import if condition is correct

Steve Holden steve at holdenweb.com
Sat Oct 28 08:07:39 EDT 2006


MindClass wrote:
> Steve Holden wrote:
> 
>>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.
>>
> 
> The problem is that I've to import different libraries according to the
> ORM (SQLObject, SQLAlchemy, etc)
> 
So why the need for global variables?

if MySQL_wanted:
     import MySQLdb as db
elif sqlite_wanted:
     import pysqlite as db
else:
     import psycopg2 as db

conn = db.connect(...)
...

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