Question

I got two different python script, the first one is using argparse to get some additional argument (I will call it arg.py) and the second one is my main script (main.py).
I want to call arg.py inside main.py but I don't know how to do so. I take a look at the command execfile but I didn't manage to make it work.
I try to put
execfile('arg.py -h')
In main.py but python try to find the file 'arg.py -h' which doesn't exist. Does anyone know how to do that ?

Thanks in advance

Was it helpful?

Solution

You can run it as a separate process using subprocess.call or subprocess.Popen. If you don't want to run it as a child process, then it starts to get more complicated (depending on the structure of your arg.py script.

execfile takes the file you pass to it and includes it in the current script -- Much like a #include pre-processor directive in c/c++ (although this is done dynamically). One really ugly option using execfile would be:

#completely untested.
import sys
def run_script(script_name,*args):
    _argv = sys.argv[:]
    sys.argv = list(args)
    execfile(script_name)
    sys.argv = _argv
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top