Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed4.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'scripts': 0.03; 'subject:file': 0.07; 'sys': 0.07; 'executable': 0.09; 'executed': 0.09; 'subject:script': 0.09; 'argument,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'garg': 0.16; 'sys.exit(1)': 0.16; 'wrote:': 0.18; 'command': 0.22; 'import': 0.22; 'mon,': 0.24; 'script': 0.25; 'pass': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'rest': 0.29; 'message-id:@mail.gmail.com': 0.30; '25,': 0.31; 'follows': 0.31; 'run': 0.32; 'another': 0.32; 'linux': 0.33; 'subject:from': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'like,': 0.36; 'should': 0.36; 'two': 0.37; 'nov': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'special': 0.74; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=kAF7gH+V8PDjnUDnMk9RITvilUNbkr7pUmETiFLmtSM=; b=kufzkpxSek/ASeJc6IRdKv0qZiwmPDREbR10rD2O68d1Qy+7oBuZOIZpIierlUW+Pf SgrbxKZarCWhpjloaYIElq6kaRKw3ancB0WEdWymAgb7C585PUNzMpUzPQHldSO39mt5 qW4khIpdGCJuNp6Thaj+EMw1PwUlfNNua3SN5OwwF1HH+CZCabqb0MndbVUP1lG1QBsL YF8BnC8qD7SMqFNhHVeBJ/Nt3CvJY2UR40q1zyVqxte6gfzpEFaEdLknghncTR2D+kMe wdQ9GsoRngsVKUTuV1wh02J/NnQ4BJdoL/a1YBuHdiC0rQZQeRvfgkvScqOjj50u7bD8 vaog== MIME-Version: 1.0 X-Received: by 10.66.150.41 with SMTP id uf9mr25415759pab.108.1385349339032; Sun, 24 Nov 2013 19:15:39 -0800 (PST) In-Reply-To: <989ee1b9-141a-4cb3-a9a2-f1527c0d0db3@googlegroups.com> References: <989ee1b9-141a-4cb3-a9a2-f1527c0d0db3@googlegroups.com> Date: Mon, 25 Nov 2013 14:15:38 +1100 Subject: Re: Excute script only from another file From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385349348 news.xs4all.nl 15970 [2001:888:2000:d::a6]:42515 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60396 On Mon, Nov 25, 2013 at 12:55 PM, 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 Easy! Just have your subprocess.call pass a special argument, which you then look for in script2: subprocess.call(["python", "script2.py","confirm"]) # script2.py import sys if "confirm" not in sys.argv: print("This script should not be run directly.") sys.exit(1) # rest of script2.py follows ChrisA