473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.

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
3 1426
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Torsten Bronger | last post by:
Hallöchen! I have a file that looks a little bit like a C header file with a long list of variables (actually constants) definitions, e.g. VI_ATTR_TIMO = 0x54378 .... Actually I need this...
3
by: Jim | last post by:
I would be very grateful for help on the following. I have the following modules in a program. Names changed to protect the innocent. 1.Simulation 2.Branches 3.MyFiles I import Branches,...
5
by: Pekka Niiranen | last post by:
Hi there, I have two scripts. The first "main.py" sets some variables and then imports another called "gen.py". The idea is to provide "main.py" that defines some paths, variables etc. without...
16
by: didier.doussaud | last post by:
I have a stange side effect in my project : in my project I need to write "gobal" to use global symbol : .... import math .... def f() : global math # necessary ?????? else next line...
23
by: Shane Hathaway | last post by:
Here's a heretical idea. I'd like a way to import modules at the point where I need the functionality, rather than remember to import ahead of time. This might eliminate a step in my coding...
5
by: Ian Bicking | last post by:
I got a puzzler for y'all. I want to allow the editing of functions in-place. I won't go into the reason (it's for HTConsole -- http://blog.ianbicking.org/introducing-htconsole.html), except that...
11
by: Brian Blazer | last post by:
OK, I have a very simple class here: class Student: """Defines the student class""" def __init__(self, lName, fName, mi): self.lName = lName self.fName = fName self.mi = mi
49
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the...
6
by: beginner | last post by:
Hi Everyone, Is there any equivalent version of C's static function in Python. I know I can make a class function private by starting a function name with two underscores, but it does not work...
4
by: bvdp | last post by:
Terry Reedy wrote: <snip> <snip> <snip>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.