I’m having difficulty with portability between Linux and MacOS with the subprocess.run()
method.
On Linux, a command like below works just fine as long as someCommand
is in my PATH
(set in ~/.bashrc
).
subprocess.run(["someCommand", "foo", "bar"])
However, on MacOS, the same command does not run, even though someCommand
is in my PATH
. Instead, I get an error like:
FileNotFoundError: [Errno 2] No such file or directory: 'someCommand'
There are many other answers on StackExchange which say “just use shell=True
and convert the args list to a string”, which seems like a fine solution for me, but I am curious why Python on MacOS doesn’t seem to inherit PATH
from the environment, while it clearly does on Linux. Can anyone clarify this for me, and/or point me to some relevant docs?
Advertisement
Answer
The problem on my Mac was that all the executables in my PATH
were declared in ~/.bash_profile
instead of ~/.bashrc
.
I never knew this before today, but .bash_profile
is only sourced on login shells, so I guess Python spawns a brand new non-login shell and doesn’t inherit the current shell environment that it was run in.
Thank you again to everyone who tried to help, I would have given up if it wasn’t for everyone’s comments!