Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'scripts': 0.03; 'subject:file': 0.07; 'executable': 0.09; 'executed': 0.09; 'function,': 0.09; 'function:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:script': 0.09; 'toplevel': 0.09; 'def': 0.12; '"hello': 0.16; 'garg': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'command': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'script': 0.25; 'header:X-Complaints- To:1': 0.27; 'code': 0.31; 'invoke': 0.31; 'another': 0.32; 'linux': 0.33; 'subject:from': 0.34; 'but': 0.35; 'there': 0.35; 'like,': 0.36; 'should': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'new': 0.61; 'name': 0.63; 'pick': 0.64 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Excute script only from another file Date: Mon, 25 Nov 2013 09:12:22 +0100 Organization: None References: <989ee1b9-141a-4cb3-a9a2-f1527c0d0db3@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084ac98.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385367108 news.xs4all.nl 15928 [2001:888:2000:d::a6]:53366 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60402 Himanshu Garg wrote: > I want that a script should only be executed when it is called from > another script and should not be directly executable through linux command > line. > > Like, I have two scripts "scrip1.py" and "script2.py" and there is a line > in "script1.py" to call "script2.py" as subprocess.call(["python", > "script2.py"]). > > Then this is should call script2 but I should not be able to directly call > script2 as $python script2.py Put the toplevel code into a function, i. e. change #script2.py old print "hello from script2" to # script2.py new def f(): # you should pick a descriptive name instead of f print "hello from script2" and then import it from script1 and invoke the function: # script1.py old #... subprocess.call("python", "script2.py") #... # script1.py new import script1 #... script.f() #...