1

I have a Python script script1 that have multiple arguments which could be simplified as:

def add_vars(var1, var2, var3, var4, var5):
    sum = var1+var2+var3+var4+var5
    return sum

if __name__ == '__main__':
    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description="""                                              
    simple addition                                                                                                                              
    """)

    parser.add_argument('var1',  type=float, help='var1')
    parser.add_argument('-var2', type=float, default=20, help='var2')
    parser.add_argument('-var3', type=float, default=30, help='var3')
    parser.add_argument('-var4', type=float, default=40, help='var4')
    parser.add_argument('-var5', type=float, default=50, help='var5')

    args = parser.parse_args()
    print(args)

    ss = add_vars(args.var1, args.var2, args.var3, args.var4, args.var5)
    print('sum=', ss)

in which only arg1 is required and arg2-arg5 are optional.

I would like to call this script in another Python script with arg1, arg3 just like in the terminal:

script1.py 1 -var3 4

Does anyone know how to do this? I have tried os.execl but without luck.

1

2 Answers 2

1

EDIT: I'd say using subprocess wouldn't be a "better" way but this is a way you could do it?

from subprocess import check_output
out = check_output(["python", "script1.py", "1", "-var3", "4"])

print(out.decode("utf-8"))

Output:

Namespace(var1=1.0, var2=20, var3=4.0, var4=40, var5=50)
sum= 115.0
<empty line>

Original:

script1.py:

def add_vars(var1, var2=20, var3=30, var4=40, var5=50):
    sum = var1+var2+var3+var4+var5
    return sum


if __name__ == '__main__':
    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
                                     description="""simple addition""")

    parser.add_argument('var1',  type=float, help='var1')
    parser.add_argument('-var2', type=float, default=20, help='var2')
    parser.add_argument('-var3', type=float, default=30, help='var3')
    parser.add_argument('-var4', type=float, default=40, help='var4')
    parser.add_argument('-var5', type=float, default=50, help='var5')

    args = parser.parse_args()
    print(args)

    ss = add_vars(args.var1, args.var2, args.var3, args.var4, args.var5)
    print('sum=', ss)

script2.py:

import script1

print(script1.add_vars(1, var3=4))

var2=20, var3=30, var4=40, var5=50 sets the default values for the function (just like for argparse)

Sign up to request clarification or add additional context in comments.

1 Comment

Excellent. Is there a better way to achieve the same? Using subprocess or so
1

if __name__=='__main__' resists your program functions to be called in other program so you can do something like this in in other program

from script1.py import add_vars

and since you have given arg 1 and 3 default vals in the function you with other arguments so you have to be careful while passing other args , while you are in the other file

add_vars(arg1,arg3)

if you would have simply call script1.py then this would have not executed

if you have further question do add a comment i'll do make my best to answer your query

if you want to change vals of arg1 and arg3

add_vars(arg1=29,arg3=8)

you can specify them , so that their default value is overrided

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.