Python subprocess can't find Pythonpath module

Question:

I am trying to use subprocess.run(['python3.9', "scripts/example.py"], check=True).

example.py uses a module, that I have added to the PYTHONPATH.
However,
whenever I run the above line, the module is not found.

The confusing part for me is, that printing sys.path inside of example.py I do see the path to my module.
But when I am running os.system("which python") or os.system("echo $PYTHONPATH") inside example.py, it returns/prints nothing.

Asked By: mayool

||

Answers:

Looks like you need to check the doc for the env parameter of subprocess.run and set it appropriately.

Side note: typically you would want to use the exact same Python interpreter for the sub-process call, so you would write: subprocess.run([sys.executable, 'scripts/example.py'], ...), unless of course you really do want 'python3.9' explicitly and nothing else (which would be surprising).

Answered By: sinoroc
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.