Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command line arguments to a python file from a script

I have a file 'train.py' which takes command line arguments to run. Some arguments are necessary and some are optional

For Example.

.\train.py --counter arg1 --opt optional_arg

Now, I would like to write a new file called 'script.py' which will import this 'train.py' and pass the argumets in that script and execute 'train.py' in 'script.py'

I need to do this as I want to give different set of arguments each time to train.py.

I know we can do this using a shell script. I am curious about how to do it using a python script.

train.py

import optparse
optparser = optparse.OptionParser()

optparser.add_option(
    "--counter", default = "",
    help = "Counter value"
)

optparser.add_option(
    "--opt", default = "",
    help = "Optinal parameter"
)

'''Do Something'''

script.py

args = {
    '--counter':''
    '--opts':''
}

lst = [ 1, 2, 3]

for counter in lst:
    '''set command line arguments here in args dictionary'''
    args['--counter'] = counter

    '''run train.py with args as input dictionary'''
like image 611
Parshuram Thorat Avatar asked Jan 30 '23 05:01

Parshuram Thorat


2 Answers

One way is to use the os module and command line via Python:

script.py

import os

# define some args
arg1 = '74.2'
optional_arg = 'disable'

os.system("python train.py --counter" + arg1 + "--opt" + optional_arg)
like image 50
collector Avatar answered Jan 31 '23 18:01

collector


One approach would be to define the actions in train.py as a function:

def train(counter, opt=None):
    '''Do Something'''

From here, in the same file, you can a check for main and run the function with the command line args.

if __name__ == '__main__':
    import optparse
    optparser = optparse.OptionParser()

    optparser.add_option(
        "--counter", default = "",
        help = "Counter value"
    )

    optparser.add_option(
        "--opt", default = "",
        help = "Optional parameter"
    )

    train(counter, opt)

Now you can import the call and train function as you would any other python package. Assuming the files are in the same directory, that could look something like:

from train import train

args = {
    '--counter':''
    '--opts':''
}

lst = [ 1, 2, 3]

for counter in lst:
    '''set command line arguments here in args dictionary'''
    kwargs['--counter'] = counter  # changed to kwargs from OP's args
                                   # for the sake of accurate nomenclature
    train(**kwargs)

Please note that unpacking the dictionary with ** only works if the key values are strings whose values are identical to the name of the function parameters.

One benefit from this approach is that you can run train independently from a command script or you can run it from a python program without the os intermediary.

like image 37
Eric Ed Lohmar Avatar answered Jan 31 '23 18:01

Eric Ed Lohmar