Parshuram Thorat
Parshuram Thorat

Reputation: 81

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'''

Upvotes: 5

Views: 12588

Answers (4)

TheIvoryCoder
TheIvoryCoder

Reputation: 57

You can use argv[] from the sys module. For example:
main.py

import sys

print(sys.argv[1])

Terminal:

python.exe main.py "hello world!"

Output:

hello world!

Upvotes: 0

TARUN KUMAR
TARUN KUMAR

Reputation: 141

Use https://docs.python.org/2/library/argparse.html

The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

Upvotes: 0

collector
collector

Reputation: 990

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)

Upvotes: 5

Eric Ed Lohmar
Eric Ed Lohmar

Reputation: 1922

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.

Upvotes: 4

Related Questions