函数subprocess.lun中的env参数做什么?

发布于 2025-02-08 16:26:41 字数 513 浏览 1 评论 0原文

在浏览子过程模块文档时,我遇到了功能运行,并且不确定其env> env参数。这是官方文档:::

如果 env 不是,它必须是定义的映射 新过程的环境变量;这些是用而不是 继承当前过程环境的默认行为。 它直接传递到popen

有人可以在我坚持的代码的背景下向我解释这个概念:

import subprocess as sp
foo= sp.run('bash ./foo_1/run_eval.sh foo_3.csv'.split(), env=os.environ.copy())

While going over the subprocess module documentation, I came across the function run and am not sure of its env argument. Here is the definition from the official documentation:

If env is not None, it must be a mapping that defines the
environment variables for the new process; these are used instead of
the default behavior of inheriting the current process’ environment.
It is passed directly to Popen.

Can someone explain this concept to me in the context of the code that I'm stuck on:

import subprocess as sp
foo= sp.run('bash ./foo_1/run_eval.sh foo_3.csv'.split(), env=os.environ.copy())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月光色 2025-02-15 16:26:41

它的目的是允许您在默认环境中覆盖元素。

换句话说,

subprocess.run("something", env=os.environ.copy())

完全等同于

subprocess.run("something")

您想在环境中覆盖某些东西可能有用的时候。

myenv = os.environ.copy()
myenv["PATH"] = "%s:%s" % ("/opt/acme/ajax/bin", os.environ["PATH"]))
myenv["SOMETHING_DEFAULTS"] = "increase_frobotzity;crash_less=True"
subprocess.run("something", env=myenv)

Its purpose is to allow you to override elements in the default environment.

In other words,

subprocess.run("something", env=os.environ.copy())

is exactly equivalent to just

subprocess.run("something")

It's when you want to override something in the environment that this might come in useful.

myenv = os.environ.copy()
myenv["PATH"] = "%s:%s" % ("/opt/acme/ajax/bin", os.environ["PATH"]))
myenv["SOMETHING_DEFAULTS"] = "increase_frobotzity;crash_less=True"
subprocess.run("something", env=myenv)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文