进程的环境变量
我需要在 python 脚本中启动进程,因此我使用这个 subprocess.Popen()
并通过 Popen
函数的参数 env
提供环境变量,但我的进程没有看到所需的环境变量。 我该怎么做呢? 任何帮助将不胜感激。
我运行的是 OS X 10.5。
例子:
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,出于安全原因,
sudo
会重置其运行的程序的环境。如果您想避免这种情况,则需要将-E
标志传递给sudo
,并且您正在运行的命令必须具有SETENV< /code> 标签或
setenv
选项必须在sudoers
文件中设置。请参阅sudo
手册页 和sudoers
手册页了解更多信息。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 tosudo
, and either the command you're running has to have theSETENV
tag or thesetenv
option has to be set in thesudoers
file. See thesudo
man page and thesudoers
man page for more information.尝试从您正在调用的程序中记录或打印出所有可用的环境变量,也许它在那里但有所不同。例如,在 Windows 中,环境变量最终全部为大写。
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.