开发者

Environment Variable for Process

开发者 https://www.devze.com 2023-02-08 17:07 出处:网络
I need to start process within python script, so I use for this subprocess.Popen() and give environment variables through parameter env of Popen function, but my process doesn\'t see needed environmen

I need to start process within python script, so I use for this subprocess.Popen() and give environment variables through parameter env of Popen function, but my process doesn't see needed environment variables. How can I do it? Any help will be appreciated.

I'm running OS X 10.5.

Example:

env = os.environ
env["Foo"] = foo

cmd = "/usr/bin/sudo -H -u "+ self.getCurrentUserName() + "-P" + +os.path.join(dir, app) + app_args

p = subprocess开发者_运维技巧.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=dir, env=env)


By default, sudo resets the environment of the program it runs for security reasons. If you want to avoid that, you need to pass in the -E flag to sudo, and either the command you're running has to have the SETENV tag or the setenv option has to be set in the sudoers file. See the sudo man page and the sudoers man page for more information.


Try logging or printing out all of the available environment variables from within the program that you're calling, maybe it's there but different. In Windows, for example, environment variables end up being all upper-case.

0

精彩评论

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