0

I have a python script(parent_script.py) which executes another python script(child_script.py) using system() command. In this python script there is a variable(var1) whose value I want to return to or access in the parent_script.py script. My current parent_script.py is:

def call_script():

    cmd = "/usr/local/bin/python2.7 child_script.py --arg1 arg1 --arg2 arg2"
    output_code = system(cmd)

    if output_code != 0:
        print(strerror(output_code) + ' \n')
        sys.exit(1)
    # Get the value of var1 in child_script.py


if __name__ == '__main__':

    call_script()  

My current child_script.py is:

def func1():
    # bunch of other code

    var1 = '2015' # this is the value that I want to access in parent_script.py


if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Child Script')
    parser.add_argument('--arg1', required=True)
    parser.add_argument('--arg2', required=True)
    args = parser.parse_args()
    func1(args)  

How can I get the value of var1 returned to or accessed in parent_script.py?

Note: I know using subprocess I can execute the python script and get the value of var1 but in this case I am restricted to use system() only to execute the python script. Also the value of var1 in child_script is shown as a dummy value. Actual value is generated by the code that is above it and I haven't shown as it is not relevant.

4
  • Short answer: you can't. With a process, you return the value of var1 from func1. Between processes, you communicate via sockets or streams. There is no notion of accessing the values of a particular variable in another program. Commented Mar 29, 2016 at 19:55
  • 1
    Use some sort of Inter-Process Communication, like a pipe (anonymous or named), a socket, shared memory, message queue, etc. At a push you could even write it to a file. Another alternative is to revisit your design and not run a script as a child process but as a module in the same process. Commented Mar 29, 2016 at 19:56
  • @chepner: Except for the fact that they are both Python scripts. My answer below shows that you can in fact do it. Commented Mar 29, 2016 at 20:01
  • @zondo can you illustrate through an example? I have also updated my post above to show that I am passing some named arguments to my child_script Commented Mar 29, 2016 at 20:06

1 Answer 1

1

Since you are using python2.7, use execfile() instead of os.system():

sys.argv[1:] = ["--arg1", "arg1", "--arg2", "arg2"]
try:
    execfile("child_script.py")
except SystemExit as error:
    output_code = error.code
else:
    output_code = 0

Then you can use var1 normally. (This assumes that global var1 was put at the beginning of func1().)

Note, though, that execfile() does not exist in Python3.

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

7 Comments

The issue is that I have to pass some named arguments to my child script. How do I do that with this? I have updated my post above to show what is the exact cmd i am executing
then I can reference var1 in my parent_script.py?
Only if you can reference var1 in child_script.py How you child script is currently written, var1 isn't a global variable, but I'm hoping that your real script has global var1 at the beginning of the definition of func1().
I can make var1 global in child_script. SO with this if I do print var1 in my parent_script i will get the value of var1 from child script right?
If you make it global in child_script.py, it will be available in parent_script.py. Just try it and see.
|

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.