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'''
5
  • 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 Commented Nov 15, 2017 at 15:09
  • I would suggest not passing data between different pieces of python code as command line args. Any reasons behind this? Commented Nov 16, 2017 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. Commented Nov 16, 2017 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 :) Commented Nov 16, 2017 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?? Commented Dec 19, 2020 at 1:04

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

4 Comments

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 :)
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 for different concat methods and relative speeds.
@ParshuramThorat I edited my answer to make it more clear how the arguments can be variables
Ah, my bad. I should have got that. nevermind, it works! Thanks :)
4

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.

1 Comment

good answer, note the optparse is deprecated in favor of argparse
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.

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.