Excute script only from another file

Michael Torrie torriem at gmail.com
Mon Nov 25 03:58:31 CET 2013


On 11/24/2013 06: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

I'm not totally sure what you mean, but if you want to prevent the
script from doing anything when run from the command line but have it do
something when imported as a module then just put this at the beginning:

if __name__ == "__main__":
    import sys
    sys.exit(0)






More information about the Python-list mailing list