Running external module and accessing the created objects

Kene Meniru Kene.Meniru at illom.org
Tue Mar 12 00:57:28 CET 2013


Here's the answer to this question. 

The summary of the question: how to run a module (called myapp.py) from 
another module (called myappwin.py) and be able to access the namespace of 
myapp.py from myappwin.py.

------------------------------------------
# contents of myapp.py
import math

class MyApp(object):
    def __init__(self):
        super(MyApp, self).__init__()
        self.name = "MyAppName"


def testFunction():
    boke = "Smilling"
    print math.sin(1), boke
-----------------------------------------
# contents of myappwin
def test():
    dic = {}
    execfile("myapp.py", dic)
    testObj = dic["MyApp"]() # access MyApp class
    dic["testFunction"]()    # execute testFunction
    print testObj.name       # print string


test()
-----------------------------------------
# OUTPUT
$ python myappwin.py
0.841470984808 Smilling
MyAppName

-- 

Kene
::::::::::::::::::
KeMeniru at gmail.com




More information about the Python-list mailing list