How to "call" one python script from another?

4164
4
11-01-2010 01:43 PM
BryceStath
New Contributor
I would like to "call" another python script at the end of another script. 
I have looked at the import function some but without much luck.  Was wondering if anyone has an example of how they have done this.

thx in advance
0 Kudos
4 Replies
FrankPerks
New Contributor II
Hi,

If your second file is just a set of methods then, in your first script:

import script2
# just call it like any other function
script2.somefunction("ice-cream!")
However if your script is a standalone script that uses command line arguments, e.g. this:

# This is hatprinter.py script, it just prints the type of hat
# passed in, argv[1] will be the type of hat.
import sys
def main():
    """
        Do more magical things
    """
    print sys.argv
    
    # i use argv[1] as a hat.
    my_hat = argv[1]
    
    # yay hats
    print my_hat
   
def __name__ == "__main__":
    main()
And you invoke this second script via commandline arguments:

python hatprinter.py "ITS A HAT!"
(output would be "ITS A HAT")

Then modify your first script:

import sys
import script2

def main():
    """
        Do stuff
    """
    # do something with this scripts arguments if needed
    # ....
    
    # to call the second scripts
    sys.argv = [script2.__file__, "ITS A HAT"]
    script2.main()

if __name__ == "__main__":
    main()
This way the first script will modify the sys.argv (stores your command line arguments) and invokes the main() fucntion of the second script (if your script does not have a common entry point then i seriously suggest you look into (__name__ == "__main__") style for writing programs).

You can also call your second script:

import os
os.system("""python script2.py "ITS A HAT"""")

# or a better way
import subprocess
subprocess.Popen(["python", "script2.py", "ITS A HAT"])
However i really don't suggest you call other python programs like this:

a. python.exe might not be defined on the path
b. "script2.py" might not be on the path
c. Etc.

Theres a few ways to do it, let me know if its not exactly what you need.

*EDIT*

Also this question would probably be better suited to be in the geoprocessing section of the forum.
GabrielHuot-Vézina
New Contributor II
I usually use this code to import another of my scripts within one.

---------------
import os

#Add the folder containing the scripts within Python sys paths
os.sys.path.append(r'C:\Temp') #for e.g.

#Import your scripts
import Custom_script
---------------

If you have any function (def abc():  ) in your scripts you now can call them by writing:
Custom_script.abc

Just as any function within a normal library.

Have fun!
0 Kudos
kostasfifli
New Contributor
Hello. Thank you for your code. To be honest I didn't understand it well. So I will try to explain my situation. I have a code (written in python) in this one i call an other one and everything ok. i receive the results that I was looking for. BUT when I am calling an other code, doesnt work. which is the right way to call this:

import os, sys, traceback, hashlib, stat, mmap, multiprocessing, re, os.path, subprocess, commands, pipes
from multiprocessing import Process, JoinableQueue
from os import listdir
from os.path import isfile, join
from os import walk
from string import Template
inst_name  = "in14"
copy ="/users/in14"
original ="/users/experiment_in14"
num_procs = multiprocessing.cpu_count()
num_procs *= 4
files = []
subdirs = []
#import statistic14
def main ():
    pass

def get_hash(fh, sz):
        h = hashlib.sha1()
        while True:
            buf = fh.read(65536)
            if len(buf) == 0: break
            h.update(buf)
        return h.digest()
   
def chk(path, lf, copy):
    path = os.path.join(copy, path)
    path1 = os.path.join(original, path)
    st = None
    st1 = None
    try:
        st = os.lstat(path)
        st1= os.lstat(path1)
       
    except:
        lf.write("Missing from the original: " + path + "\n")
        lf.flush()
        return

    if not stat.S_ISREG(st.st_mode):
        return
   
    if st.st_size != st1.st_size:
        lf.write("c.Size differ: " + path + "\n")
        lf.flush()
        return
    if st.st_size == 0: return

    hv = None
    hv1 = None
    try:
        fh = open(path, "r")
        hv = get_hash(fh, st.st_size)
        fh.close()
        fh = open(path, "r")
        hv1 = get_hash(fh, st.st_size)
        fh.close()
    except:
        lf.write("c.Open error: " + path + "\n")
        lf.flush()
        return

    if hv != hv1:
        lf.write("c.Digests differ: " + path + "\n")
        lf.flush()

def proc_chk(q, lf,copy):
    while True:
        path = q.get()
        if path == "done":
            break
        chk(path, lf, copy)
        q.task_done()
    q.task_done()
basedir=os.getcwd()

q = JoinableQueue()
lf = open("/users/compare14", "w")
path = os.path.realpath(sys.argv[0])
path1 = os.path.realpath(sys.argv[0])

o_cwd = os.getcwd()
os.chdir(copy)
cwd = os.getcwd()

for i in range(0, num_procs):
    p = Process(target=proc_chk, args=(q, lf, original))
    p.start()

for dirpath, dirnames, filenames in os.walk(".", followlinks=False):
    for f in filenames:
        q.put(os.path.join(dirpath, f))

for i in range(0, num_procs):
    q.put("done")

q.join()
lf.close()
os.chdir(o_cwd)
##################################################################################################
def get_hash(fh, sz):
    h = hashlib.sha1()
    while True:
        buf = fh.read(65536)
        if len(buf) == 0: break
        h.update(buf)
    return h.digest()

   
def chk(path, lf, original):
    path1 = os.path.join(original, path)
    st = None
    st1 = None
    try:
        st = os.lstat(path)
        st1 = os.lstat(path1)
    except:
        lf.write("Missing from the copy: " + path1 + "\n")
        lf.flush()
        return

    if not stat.S_ISREG(st.st_mode):
        return
   
    if st.st_size != st1.st_size:
        lf.write("c.Size differ: " + path + "\n")
        lf.flush()
        return
    if st.st_size == 0: return

    hv = None
    hv1 = None
    try:
        fh = open(path, "r")
        hv = get_hash(fh, st.st_size)
        fh.close()
        fh = open(path1, "r")
        hv1 = get_hash(fh, st.st_size)
        fh.close()
    except:
        lf.write("Open error: " + path + "\n")
        lf.flush()
        return
   
    if hv != hv1:
        lf.write("c.Digests differ: " + path + "\n")
        lf.flush()
       
def proc_chk(q, lf, original):
    while True:
        path1 = q.get()
        if path1 == "done":
            break
        chk(path1, lf, original)
        q.task_done()
    q.task_done()

q = JoinableQueue()
lf = open("/users/compare14", "a+")
path = os.path.realpath(sys.argv[0])
path1 = os.path.realpath(sys.argv[0])

o_cwd = os.getcwd()
os.chdir(original)
cwd = os.getcwd()

for i in range(0, num_procs):
    p = Process(target=proc_chk, args=(q, lf, copy))
    p.start()

for dirpath, dirnames, filenames in os.walk(".", followlinks=False):
    for f in filenames:
        q.put(os.path.join(dirpath, f))

for i in range(0, num_procs):
    q.put("done")

q.join()
lf.close()
os.chdir(o_cwd) 

if __name__ == '__main__':
     main()
0 Kudos
kostasfifli
New Contributor
Hello. Thank you for your code. To be honest I didn't understand it well. So I will try to explain my situation. I have a code (written in python) in this one i call an other one and everything ok. i receive the results that I was looking for. BUT when I am calling an other code, doesnt work. which is the right way to call this:

import os, sys, traceback, hashlib, stat, mmap, multiprocessing, re, os.path, subprocess, commands, pipes
from multiprocessing import Process, JoinableQueue
from os import listdir
from os.path import isfile, join
from os import walk
from string import Template
inst_name  = "in14"
copy ="/users/in14"
original ="/users/experiment_in14"
num_procs = multiprocessing.cpu_count()
num_procs *= 4
files = []
subdirs = []
#import statistic14
def main ():
    pass

def get_hash(fh, sz):
        h = hashlib.sha1()
        while True:
            buf = fh.read(65536)
            if len(buf) == 0: break
            h.update(buf)
        return h.digest()
   
def chk(path, lf, copy):
    path = os.path.join(copy, path)
    path1 = os.path.join(original, path)
    st = None
    st1 = None
    try:
        st = os.lstat(path)
        st1= os.lstat(path1)
       
    except:
        lf.write("Missing from the original: " + path + "\n")
        lf.flush()
        return

    if not stat.S_ISREG(st.st_mode):
        return
   
    if st.st_size != st1.st_size:
        lf.write("c.Size differ: " + path + "\n")
        lf.flush()
        return
    if st.st_size == 0: return

    hv = None
    hv1 = None
    try:
        fh = open(path, "r")
        hv = get_hash(fh, st.st_size)
        fh.close()
        fh = open(path, "r")
        hv1 = get_hash(fh, st.st_size)
        fh.close()
    except:
        lf.write("c.Open error: " + path + "\n")
        lf.flush()
        return

    if hv != hv1:
        lf.write("c.Digests differ: " + path + "\n")
        lf.flush()

def proc_chk(q, lf,copy):
    while True:
        path = q.get()
        if path == "done":
            break
        chk(path, lf, copy)
        q.task_done()
    q.task_done()
basedir=os.getcwd()

q = JoinableQueue()
lf = open("/users/compare14", "w")
path = os.path.realpath(sys.argv[0])
path1 = os.path.realpath(sys.argv[0])

o_cwd = os.getcwd()
os.chdir(copy)
cwd = os.getcwd()

for i in range(0, num_procs):
    p = Process(target=proc_chk, args=(q, lf, original))
    p.start()

for dirpath, dirnames, filenames in os.walk(".", followlinks=False):
    for f in filenames:
        q.put(os.path.join(dirpath, f))

for i in range(0, num_procs):
    q.put("done")

q.join()
lf.close()
os.chdir(o_cwd)
##################################################################################################
def get_hash(fh, sz):
    h = hashlib.sha1()
    while True:
        buf = fh.read(65536)
        if len(buf) == 0: break
        h.update(buf)
    return h.digest()

   
def chk(path, lf, original):
    path1 = os.path.join(original, path)
    st = None
    st1 = None
    try:
        st = os.lstat(path)
        st1 = os.lstat(path1)
    except:
        lf.write("Missing from the copy: " + path1 + "\n")
        lf.flush()
        return

    if not stat.S_ISREG(st.st_mode):
        return
   
    if st.st_size != st1.st_size:
        lf.write("c.Size differ: " + path + "\n")
        lf.flush()
        return
    if st.st_size == 0: return

    hv = None
    hv1 = None
    try:
        fh = open(path, "r")
        hv = get_hash(fh, st.st_size)
        fh.close()
        fh = open(path1, "r")
        hv1 = get_hash(fh, st.st_size)
        fh.close()
    except:
        lf.write("Open error: " + path + "\n")
        lf.flush()
        return
   
    if hv != hv1:
        lf.write("c.Digests differ: " + path + "\n")
        lf.flush()
       
def proc_chk(q, lf, original):
    while True:
        path1 = q.get()
        if path1 == "done":
            break
        chk(path1, lf, original)
        q.task_done()
    q.task_done()

q = JoinableQueue()
lf = open("/users/compare14", "a+")
path = os.path.realpath(sys.argv[0])
path1 = os.path.realpath(sys.argv[0])

o_cwd = os.getcwd()
os.chdir(original)
cwd = os.getcwd()

for i in range(0, num_procs):
    p = Process(target=proc_chk, args=(q, lf, copy))
    p.start()

for dirpath, dirnames, filenames in os.walk(".", followlinks=False):
    for f in filenames:
        q.put(os.path.join(dirpath, f))

for i in range(0, num_procs):
    q.put("done")

q.join()
lf.close()
os.chdir(o_cwd) 

if __name__ == '__main__':
     main()
0 Kudos