Creating "virtual" module-namespace

Nicodemus nicodemus at globalite.com.br
Thu Dec 12 07:19:29 EST 2002


Harald wrote:

>I have some modules stored in strings (to be exact: in a database).
>
>Now I want to use THIS modules in the same way as if I would import them
>via 
>import FileName
>
>If I simply exec this strings in "globals"
>
>exec <string> in globals()
>
>all the classes & functions are in my global namespace - without a 
>prefix.
>
>That's all.
>
>Thank you for your time
>
>Harald
>


If I understood correctly, you have for example ['ftplib', 'mymodule']
stored in a database, and you want to, given one of the modules of the
database, use them like this:

x = mymodule.Foo()
mymodule.set('Hail')

If that is so, use the builtin function __import__:

mymodule = __import__('mymodule')

And there you go.

Hope that helps.
Nicodemus.







More information about the Python-list mailing list