By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,421 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,421 IT Pros & Developers. It's quick & easy.

Function to import module to namespace

P: n/a

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().

Thanks.
Jun 29 '08 #1
Share this Question
Share on Google+
3 Replies

P: n/a
Le Sunday 29 June 2008 21:08:36 bvdp, vous avez écrit*:
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().
You can dynamically add objects to a module:
>>import os
os.foo = 'bar'
os.foo
'bar'
>>setattr(os, 'foo2', 'bar2')
os.foo2
'bar2'

and for the loading part you can use the __import__ builtin or maybe execfile
(see the 'built-in functions' chapter of the library reference for more about
these).

--
Cédric Lucantis
Jun 29 '08 #2

P: n/a


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

Jun 29 '08 #3

P: n/a
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.
Jun 29 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.