0

I have two Python scripts, a parent script (parent.py) which passes a value to the child script through subprocess, and a child script (child.py), which uses the argument given in subprocess to return a value to the parent function. Some background: in my actual script, I read in PDF files in the parents and subprocess the child whenever the PDF is not readable. The child then OCRs the PDF (I pass the PDF path from the parent to the child through subprocess) and is supposed to return the text to the parent (this is what currently fails).

This is the code that I have, I use Python 3.9 on Windows.

parent.py

import subprocess

def main():
    a='The dog jumps. Over a fence.'
    subprocess.call(["python", "child.py", a])

main()

from child import test
child_result = test()
print(child_result)

child.py

import sys
    
def main():
    a = sys.argv[1].split('.')
    return a

def test():
    for i in main():
        output = i
        print(output)

if __name__ == '__main__':
    main()

I would like to use the variable output from the child function in the parent function, but when I try to run the code I get this error message:

Traceback (most recent call last):

  File "P:\2020\14\Kodning\Code\custodyproject\parent.py", line 10, in <module>
    child_result = test()

  File "P:\2020\14\Kodning\Code\custodyproject\child.py", line 8, in test
    for i in main():

  File "P:\2020\14\Kodning\Code\custodyproject\child.py", line 4, in main
    a = sys.argv[1].split('.')

IndexError: list index out of range

The error seems to stem from including sys.argv in the function test(); when I return just a random variable from test() to the parent it works. My question is: can I return variables from the child to the parent that are based on the subprocess arguments given in the parent?

Thank you for your help!

6
  • How did you run parent.py? Your use of subprocess.call has nothing to do with the value of sys.argv as seen by child.main. Commented Mar 2, 2022 at 13:58
  • Why are you calling Python as a subprocess of itself anyway? Commented Mar 2, 2022 at 14:03
  • You're right @chepner, that was my mistake, I forgot to change the file names and updated that. Commented Mar 2, 2022 at 14:27
  • @tripleee: I call subprocess to run the OCR script that extracts the text from the PDFs. The script above is much shorter, in my project I keep the files seperate to keep the code cleaner, but I suppose I could instead put the OCR (child) script into a function in the parent script_ Commented Mar 2, 2022 at 14:31
  • Keeping code in separate files is obviously a good idea but, again, the usual way to fetch the functions from another file is to import it. The code needs to follow a few simple conventions for that to work, but this is amply documented elsewhere. Commented Mar 3, 2022 at 6:13

1 Answer 1

0

You want to capture output of the child.

cp = subprocess.run(["python", "child.py", a], text=True, stdout=subprocess.PIPE)
# cp.returncode is the exit status of child.py
# cp.stdout is a string containing the output of child.py
Sign up to request clarification or add additional context in comments.

2 Comments

When I replace the subprocess line as you suggest and call cp.returncode I receive 0, for cp.stdout I receive None
Oh, I forgot the option to make stdout an in-process pipe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.