通过python脚本设置shell环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38982640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set shell environment variable via python script
提问by Sergey Asryan
I have some instrument which requires environment variable which I want to set automatically from python code. So I tried several ways to make it happen, but none of them were successful. Here are some examples:
我有一些需要环境变量的仪器,我想从 python 代码中自动设置它。所以我尝试了几种方法来实现它,但都没有成功。这里有些例子:
I insert following code in my python script
import os os.system("export ENV_VAR=/some_path")
I created bash script(env.sh) and run it from python:
#!/bin/bash export ENV_VAR=some_path #call it from python os.system("source env.sh")
- I also tried os.putenv()and os.environ["ENV_VAR"] = "some_path"
我在我的 python 脚本中插入以下代码
import os os.system("export ENV_VAR=/some_path")
我创建了 bash 脚本(env.sh)并从 python 运行它:
#!/bin/bash export ENV_VAR=some_path #call it from python os.system("source env.sh")
- 我也试过os.putenv()和os.environ["ENV_VAR"] = "some_path"
Is it possible to set(export) environment variable using python, i.e without directly exporting it to shell?
是否可以使用python设置(导出)环境变量,即不直接将其导出到shell?
采纳答案by Stan Prokop
As long as you start the "instrument" (a script I supposed) from the very same process it should work:
只要您从完全相同的过程中启动“工具”(我认为是脚本),它就应该可以工作:
In [1]: os.putenv("VARIABLE", "123")
In [2]: os.system("echo $VARIABLE")
123
You can't change an environment variable of a different process or a parent process.
您不能更改不同进程或父进程的环境变量。
回答by kindall
Setting an environment variable sets it only for the current process and any child processes it launches. So using os.system
will set it only for the shell that is running to execute the command you provided. When that command finishes, the shell goes away, and so does the environment variable. Setting it using os.putenv
or os.environ
has a similar effect; the environment variables are set for the Python process and any children of it.
设置环境变量仅为当前进程和它启动的任何子进程设置它。因此 usingos.system
将仅为正在运行以执行您提供的命令的 shell 设置它。当该命令完成时,shell 消失,环境变量也消失。使用os.putenv
或设置它os.environ
具有类似的效果;环境变量是为 Python 进程及其任何子进程设置的。
I assume you are trying to have those variables set for the shell that you launch the script from, or globally. That can't work because the shell (or other process) is not a child of the Python script in which you are setting the variable.
我假设您正在尝试为从中或全局启动脚本的 shell 设置这些变量。这是行不通的,因为 shell(或其他进程)不是您在其中设置变量的 Python 脚本的子进程。
You'll have better luck setting the variables in a shell script. If you then source
that script (so that it runs in the current instance of the shell, rather than in a subshell) then they will remain set after the script ends.
在 shell 脚本中设置变量会更好。如果您然后使用source
该脚本(以便它在 shell 的当前实例中运行,而不是在子 shell 中运行),那么它们将在脚本结束后保持设置。
回答by mike.dld
Depending on how you execute your instrument, you might be able to change environment specifically for the child process without affecting the parent. See documentation for os.spawn*e
or subprocess.Popen
which accept separate argument denoting child environment. For example, Replacing the os.spawn familyin subprocess
module documentation which provides both usages:
根据您执行仪器的方式,您可能能够专门为子进程更改环境,而不会影响父进程。请参阅有关os.spawn*e
或subprocess.Popen
接受表示子环境的单独参数的文档。例如,在提供两种用法的模块文档中替换 os.spawn 系列subprocess
:
Environment example:
os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env) ==> Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})
环境示例:
os.spawnlpe(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg", env) ==> Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})