I assume that module's name is "module.filename.py.txt", the content follows.
# module.filename.py.txt
def hello():
for i in range(3):
print 'oO',
print
The interactive interpreter:
Python 2.6.5 (r265:79063, Jun 3 2010, 14:39:13)
[GCC 4.1.2 20090703] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Module(object):
... """Module object."""
... def __init__(self, ns_dict):
... """Populate the module."""
... self.__dict__ = ns_dict
...
>>> mod_filename = 'module.filename.py.txt'
>>> mod_ns_alias = 'mymodule'
>>> source = open(mod_filename).read()
>>> module_code = compile(source, mod_filename, 'exec')
>>> mod_ns = {}
>>> exec module_code in mod_ns
>>> exec('%s = Module(mod_ns)' % mod_ns_alias)
>>>
>>> mymodule.hello()
oO oO oO
>>>

0 comments:
Post a Comment