Nika Neugier
Nika Neugier

Reputation: 1

Environment Variable for Process

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)

Upvotes: 0

Views: 1526

Answers (2)

Adam Rosenfield
Adam Rosenfield

Reputation: 400156

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.

Upvotes: 4

Dave
Dave

Reputation: 2089

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.

Upvotes: 0

Related Questions