是否可以永久地从Python设置环境变量? [英] Is it possible to set an environment variable from Python permanently?
问题描述
使用 os
模块我可以获取环境变量的值。例如:
os.environ ['HOME']
但是,我无法设置环境变量:
os.environ ['BLA'] =FOO
它在程序的当前会话中工作,但是当我python程序完成后,我看不到它改变(或设置)环境变量的值。有没有办法从Python的办呢?
如果你想要的是让你的环境变量坚持翻过会话,可以
对于 unix
在 bash
shell。在〜/ .bashrc
中附加环境变量。
import os
与开放(os.path.expanduser( 〜/ .bashrc中), A)作为OUTFILE:
# 'A' 代表 追加
outfile.write(出口MYVAR = MYVALUE)
或 Windows :
SETX / M MYVAR myvalue的
在$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
Using os
module I can get the values of the environment variables. For example:
os.environ['HOME']
However, I cannot set the environment variables:
os.environ['BLA'] = "FOO"
It works in the current session of the program but when I python program is finished, I do not see that it changed (or set) values of the environment variables. Is there a way to do it from Python?
If what you want is to make your environment variables persist accross sessions, you could
For unix
do what we do when in bash
shell. Append you environment variables inside the ~/.bashrc
.
import os
with open(os.path.expanduser("~/.bashrc"), "a") as outfile:
# 'a' stands for "append"
outfile.write("export MYVAR=MYVALUE")
or for Windows:
setx /M MYVAR "MYVALUE"
in a *.bat
that is in Startup in Program Files
这篇关于是否可以永久地从Python设置环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!