How to modify the Python path for a subprocess

Viewed 418

I am trying to run a Python script child.py from parent.py, but child.py imports modules that are not present in the same directory. So child.py first sets the path using sys.path.append().

I want to set the path from parent.py and then run child.py from my script. The reason is the name of the directory where the modules are present keeps changing according to the version updates. I am trying to run sys.path.append() from parent.py but it is not reflecting in child.py.

How do I solve this?

parent.py

import sys

sys.path.append("/path/to/directory-v2")
proc = subprocess.popen(["python3", "child.py"], stdout=subprocess.PIPE)
...

child.py

import sys

sys.path.append("/path/to/directory-v1")
import module1, module2
...
0 Answers
Related