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

Question:

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'''
Asked By: Parshuram Thorat

||

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)
Answered By: 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.

Answered By: Eric Ed Lohmar

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.

Answered By: TARUN KUMAR

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!
Answered By: Charlie Cureton
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.