If all you need is your Python code running in a separate process, you probably shouldn't use subprocess . As Serge Ballesta said, the goal of subprocess is to run another program at the OS level without really worrying about what it is - you have nothing to help you deal with the intricacies of the python interpreter process.
For this purpose, it is best to simply import your code and use multiprocessing , which provides a high-level interface that will help you run python code in multiple processes.
Assuming you have a specific main function on example.py :
from examply import main as example_main import multiprocessing pool= multiprocessing.Pool(1) pool.apply( example_main )
Using this code, both exceptions and return values ββwill be transparently passed to your main process.
You can also use Pool.apply_async if you do not want to block waiting for the result.
source share