Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.04; 'python': 0.09; 'namespace': 0.09; 'subject:module': 0.09; 'def': 0.10; "hasn't": 0.15; 'subject:accessing': 0.16; 'subject:created': 0.16; 'test()': 0.16; 'test():': 0.16; 'string': 0.17; 'wrote:': 0.17; 'script.': 0.17; 'module': 0.19; 'math': 0.20; 'question.': 0.20; 'skip:- 40': 0.21; 'import': 0.21; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'run': 0.28; 'question:': 0.29; 'summary': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'print': 0.32; 'to:addr:python-list': 0.33; 'another': 0.33; 'skip:d 20': 0.34; 'pm,': 0.35; 'execute': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'most': 0.61; 'dangerous': 0.66; 'received:74.208': 0.71; 'dic': 0.84 Date: Mon, 11 Mar 2013 20:48:43 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130221 Thunderbird/17.0.3 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Running external module and accessing the created objects References: <513aecd3$0$6512$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:kRbPM2w1hQpMX5fGmJ2N+sCUhDOXp41zLc0n3TUknw2 J+KLUiV6RMerjiL5p51YeHybeb+OdyHmCAQMFXL1kKaeXF9njv DPxgJaJGrGfc4yctDgrGq+ZABuVhHjCmVqlrBT4OPzEeg9j12A yRWJcOyAtySQOKueyjt9xNU0vW4VLFr4S0poc95ws4FP4Oqi9V 9Ml771avX1hh92D/qZPI5YqaupGJWqNG9KLHfiPsUwHbE4AS1V Cwo8babCAQDs4mWJrdZbYaPrihH/Ocv/z753/fhI2e/d11R4KV chkkoNrw7FI6YENZCcuO92veNs3ZwYDZm5svYOVCoxMUd86dA= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363049352 news.xs4all.nl 6877 [2001:888:2000:d::a6]:57418 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41101 On 03/11/2013 07:57 PM, Kene Meniru wrote: > 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 > I hope you're just kidding. execfile() and exec() are two of the most dangerous mechanisms around. import or __import__() would be much better, as long as your user hasn't already run myapp.py as his script. -- DaveA