Re: Function to import module to namespace





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

.



Relevant Pages

  • Re: Function to import module to namespace
    ... the defs in that module to an existing module/namesapce. ... import_module(n, mods) ... (see the 'built-in functions' chapter of the library reference for more about ...
    (comp.lang.python)
  • Re: Function to import module to namespace
    ... Terry Reedy wrote: ... bvdp wrote: ... the defs in that module to an existing module/namesapce. ... import_module(n, mods) ...
    (comp.lang.python)
  • Function to import module to namespace
    ... Is it possible to do this from a function: import a module and append the defs in that module to an existing module/namesapce. ... import_module(n, mods) ... Assuming that foo1is defined in newmod, I should now be able to do something like mods.foo1. ...
    (comp.lang.python)