472,364 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 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 1370
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>
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.