Function to import module to namespace
bvdp
bob at mellowood.ca
Sun Jun 29 19:00:09 EDT 2008
Terry Reedy wrote:
>
>
> bvdp wrote:
>>
>> Is it possible to do this from a function: import a module and append
>> the defs in that module to an existing module/namesapce.
>>
>> So, in my code I have something like:
>>
>> # main code
>> import mods
>>
>> def loadmore(n):
>> import_module(n, mods)
>>
>> ....
>> # end of main
>>
>> this will permit the addition of the the stuff in file 'n.py' to 'mods'.
>>
>> Assuming that foo1() is defined in newmod, I should now be able to do
>> something like mods.foo1().
>
> Do you mean something like this?
> >>> import string
> >>> dir(string)
> ['Formatter', 'Template', '_TemplateMetaclass', '__builtins__',
> '__doc__', '__file__', '__name__', '__package__', '_multimap', '_re',
> 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'capwords',
> 'digits', 'hexdigits', 'maketrans', 'octdigits', 'printable',
> 'punctuation', 'whitespace']
> >>> import math
>
> >>> math.__dict__.update(string.__dict__)
> >>> dir(math)
> ['Formatter', 'Template', '_TemplateMetaclass', '__builtins__',
> '__doc__', '__file__', '__name__', '__package__', '_multimap', '_re',
> 'acos', 'acosh', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase',
> 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'capwords', 'ceil',
> 'copysign', 'cos', 'cosh', 'degrees', 'digits', 'e', 'exp', 'fabs',
> 'factorial', 'floor', 'fmod', 'frexp', 'hexdigits', 'hypot', 'isinf',
> 'isnan', 'ldexp', 'log', 'log10', 'log1p', 'maketrans', 'modf',
> 'octdigits', 'pi', 'pow', 'printable', 'punctuation', 'radians', 'sin',
> 'sinh', 'sqrt', 'sum', 'tan', 'tanh', 'trunc', 'whitespace']
>
> tjr
>
Yes, I think that's what I might need. I'll give it a go in my code and
see if that does work.
Thanks.
More information about the Python-list
mailing list