Re: Function to import module to namespace
- From: Terry Reedy <tjreedy@xxxxxxxx>
- Date: Sun, 29 Jun 2008 18:44:50 -0400
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
.
- Follow-Ups:
- Re: Function to import module to namespace
- From: bvdp
- Re: Function to import module to namespace
- References:
- Function to import module to namespace
- From: bvdp
- Function to import module to namespace
- Prev by Date: Re: using urllib2
- Next by Date: Re: UnboundLocalError problems
- Previous by thread: Re: Function to import module to namespace
- Next by thread: Re: Function to import module to namespace
- Index(es):
Relevant Pages
|