0

I'm writing a code in which inside of it I need to run another python file which I have the path for it , and to save its output ( the exception it returns or the output it self ). Can someone guide me ? I tried subprocess but it doesn't run :

import subprocess    
output = call ('python' , filePath)

thanks in advance

2

1 Answer 1

1

Short answer is don't. This is not the intended way to call python code from another python program.

If you want to run functionality from a second python function you should import the other file and call a function.

For example

#MainProg.py
import OtherProg

result = OtherProg.add(2,2)
print(result)

.

#OtherProg.py
def add(num1, num2):
    return num1 + num2
Sign up to request clarification or add additional context in comments.

1 Comment

There are many Python programs that are not suitable for import by other programs – either because they would interfere with the program state (e.g. library versions) or only expose a CLI.

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.