how to pass arguments to imported script in Python
Asked Answered
V

2

8

I have a script (script1.py) of the following form:

#!/bin/python

import sys

def main():
    print("number of command line options: {numberOfOptions}".format(numberOfOptions = len(sys.argv)))
    print("list object of all command line options: {listOfOptions}".format(listOfOptions = sys.argv))
    for i in range(0, len(sys.argv)):
        print("option {i}: {option}".format(i = i, option = sys.argv[i]))

if __name__ == '__main__':
    main()

I want to import this script in another script (script2.py) and pass to it some arguments. The script script2.py could look something like this:

import script1

listOfOptions = ['option1', 'option2']
#script1.main(listOfOptions) insert magic here

How could I pass the arguments defined in script2.py to the main function of script1.py as though they were command line options?

So, for example, would it be Pythonic to do something such as the following?:

import script1
import sys

sys.argv = ['option1', 'option2']
script1.main()
Vanadinite answered 11/6, 2014 at 18:23 Comment(2)
possible duplicate of Python dynamically importing a script, need to have its __name__ == "__main__" code to be calledTextbook
return the desired value from the main function of script1 and call it in script2Postrider
E
1

Separate command line parsing and called function

For reusability of your code, it is practical to keep the acting function separated from command line parsing

scrmodule.py

def fun(a, b):
    # possibly do something here
    return a + b

def main():
    #process command line argumens
    a = 1 #will be read from command line
    b = 2 #will be read from command line
    # call fun()
    res = fun(a, b)
    print "a", a
    print "b", b
    print "result is", res

if __name__ == "__main__":
    main()

Reusing it from another place

from scrmodule import fun

print "1 + 2 = ", fun(1, 2)
Exine answered 11/6, 2014 at 18:38 Comment(2)
Thank you very much. This is very good advice. Unhappily, script1.py, which contains the main processing in the function main(), represents some legacy code that I cannot change.Vanadinite
@Vanadinite - Now I see. Your approach with setting value to sys.argv looks good. In case you cannot modify the legacy script, it could be a way to go.Exine
L
0
# script1.py
#!/bin/python

import sys

#main function is expecting argument from test_passing_arg_to_module.py code.
def main(my_passing_arg):
    print("number of command line options:  {numberOfOptions}".format(numberOfOptions = len(sys.argv)))
    print("list object of all command line options: {listOfOptions}".format(listOfOptions = my_passing_arg))
    print(my_passing_arg)
if __name__ == '__main__':
    main()

#test_passing_arg_to_module.py
import script1
my_passing_arg="Hello world"
#calling main() function from script1.py code.
#pass my_passinga_arg variable to main(my_passing_arg) function in scritp1.py. 
script1.main(my_passing_arg)
##################
# Execute script
# $python3.7 test_passing_arg_to_module.py
# Results.
# number of command line options:  1
# list object of all command line options: Hello world
# Hello world


Lingam answered 17/4, 2021 at 19:3 Comment(2)
This answer does not explain why it works, how can one learn from it? Also, you have merged the interpreter code with file code, please edit your post and explanationAnglophobia
You've modified main() to accept arguments, which doesn't help when trying to do this with an imported module where users can't modify.Thelma

© 2022 - 2024 — McMap. All rights reserved.