开发者

/bin/sh - non interactive usage from Python

开发者 https://www.devze.com 2023-03-12 03:28 出处:网络
I am invoking开发者_开发知识库 cmd from Python like this: subpocess.Popen([\'coffee\'], shell=True)

I am invoking开发者_开发知识库 cmd from Python like this:

subpocess.Popen(['coffee'], shell=True)

which I belive is translated to:

/bin/sh -c "coffee"

From docs I have read that in non-interactive mode files like /etc/profile, /etc/bash.bashrc are not read and default $PATH is used (init $PATH). Am I right? Is there the only way to add coffee to the $PATH is to copy it to /usr/local/bin?


No, the shell will inherit the PATH from the Python interpreter, i.e. it will be os.getenv('PATH'). Also, you can set the path within the command:

subprocess.Popen(['PATH=/where/ever/bin:$PATH coffee'], shell=True)

though I'd really advise you to either use the full path to coffee, or set the PATH before executing your Python program.


Historically, none of the startup files are read for shells invoked as non-interactive commands, since presumably the environment is already set up.

Python, shells, and in fact almost everything will pass the existing environment or at least the PATH environment variable through to the child shell and so, in a way, you have already executed .profile or a bash extension. (Because it was run when the user logged in and other processes inherited the modified PATH.)

Options you have:

  • specify a path for all system users in /etc/profile
  • require appropriate PATH settings from the user (you inherit them)
  • run subprocess utilities with full pathnames
  • put all commands in directories already on the PATH
0

精彩评论

暂无评论...
验证码 换一张
取 消