dynamically import

Mike Zamansky zamansky at groo.stuy.edu
Sat Feb 1 16:15:20 EST 2003


Reid Lai <reidlai at reidlai.com> writes:

> Dear All,
> 
> How can I have a database connection depending on different DBI 2.0
> module dynamically?  What I want is like following:
> 
> a = "DBVendor"
> 
> if DBVendor == "PostgreSQL":
>      from pyPgSQL import PgSQL
>      connfactory = PgSQL
> elsif DBVendor == "Oracle":
>      from DCOracle2 import *
>      connfactory = DCOracle2
> 

You could do somethign like the following:

my_module=__import__(module_name)

then later, you can use it:

x=my_module.functionname(etc)




Mike




More information about the Python-list mailing list