5

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'''
Parshuram Thorat
  • 101
  • 1
  • 2
  • 13
  • 3
    Although what you ask for is possible, I would suggest not passing data between different pieces of python code as command line args. Instead wrap the functionality of one script into a function or class, and import into the other script. If you want to be able to keep using the files as scripts, you can use the ``if __name__ == "__main__"`` idiom – James Elderfield Nov 15 '17 at 15:09
  • ***I would suggest not passing data between different pieces of python code as command line args***. Any reasons behind this? – Parshuram Thorat Nov 16 '17 at 16:46
  • Passing data as command line args requires you to encode that data as a string. While this may be okay for your current design or when the data is simple, it can quickly become tedious. Ultimately it seems a bit pointless (and inefficient) to convert data to a string and then back to the original type, when you have the option to pass it directly. – James Elderfield Nov 16 '17 at 17:44
  • 1
    Ah, I see. that makes sense. I actually need it as both strings and as an integer. But neverthless, I will keep this in mind for future. Thanks :) – Parshuram Thorat Nov 16 '17 at 18:13
  • Hi. I have a complex Flask application master.py which I cannot get to run in Python3 . I need to run a Python3 program using sockets to pass data to another SYSTEM for action. Parameters seems to be the only way to do this?? – ianm Dec 19 '20 at 01:04

4 Answers4

5

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)
Parshuram Thorat
  • 101
  • 1
  • 2
  • 13
collector
  • 940
  • 1
  • 8
  • 14
  • I don't think it will work since you are passing it a string to os.system. I am just skeptical about whether or not it would replace arg1 with the counter value. Correct me if I am wrong. Thank you for helping btw :) – Parshuram Thorat Nov 16 '17 at 16:44
  • He used a very brief example. You could use this method, you would just have to inject your variables into the string by some means. See [this answer](https://stackoverflow.com/a/43213810/2000793) for different concat methods and relative speeds. – Eric Ed Lohmar Nov 16 '17 at 20:35
  • @ParshuramThorat I edited my answer to make it more clear how the arguments can be variables – collector Nov 16 '17 at 21:37
  • Ah, my bad. I should have got that. nevermind, it works! Thanks :) – Parshuram Thorat Nov 17 '17 at 00:12
3

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.

Eric Ed Lohmar
  • 1,832
  • 1
  • 17
  • 26
0

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.

Community
  • 1
  • 1
TARUN KUMAR
  • 141
  • 8
0

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!