Re: Function to import module to namespace
- From: Cédric Lucantis <omer@xxxxxxxxxx>
- Date: Sun, 29 Jun 2008 21:24:15 +0200
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:
'bar'import os
os.foo = 'bar'
os.foo
'bar2'setattr(os, 'foo2', 'bar2')
os.foo2
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
.
- References:
- Function to import module to namespace
- From: bvdp
- Function to import module to namespace
- Prev by Date: Re: gelato - nvidia and python
- Next by Date: Re: Docutils rst2html.py gives <Error/3> Unknown Directive type "toctree"
- Previous by thread: Function to import module to namespace
- Next by thread: Re: Function to import module to namespace
- Index(es):
Relevant Pages
|