1

I am familiar with calling external python programs from another python program.

Psudocode to declare the code structure, goes like this:

#MAIN PROGRAM
import secondary
#...
is_OK = secondary.wrapper_function(param1, param2)
if is_OK == "0":
   #do_something

and the external python program:

#SECONDARY PROGRAM
import lib1
def wrapper_function(param1, param2):
   myparam = param1 #get parameters
   #do_stuff
   return "0"

As you can see, we pass the parameters to the function.

However, in my case now, the python script that i want to call, receives its parameters from flags.

This is how you would call it.

python my_program.py --parameter1 "0.4" --parameter2 "none"

The program handles the flags with something like this:

parser.add_argument(
        "--parameter2",
        type=str,
        default="grid",
        choices=[
            # All choices are listed here
        ],
        help="defines a major parameter",
    )

I can place the code in the external program in a function, in order to call it from the main program.

My question is, how do i pass the parameters that the external program receives as flags from the terminal?

1 Answer 1

1

You have two basic options here:

  1. treat your external (Python?) programm like a black-box command line program and call it using subprocess.run.

  2. However, if you have the program's source code nearby and its internal API is cleanly separated from the CLI parser, you could import my_program and invoke its functionality directly, possibly with a custom wrapper function.

Which option is better depends completely on how strong or weak you want the coupling between both programs to be.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Yes, i have the code for the external program. Using subprocess is out of the question.I have already done the 2nd approach in another app (i gave the code example in my original question). But that external program received arguments as inputs. I am not sure how i can pass the terminal flags as function parameters.

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.