通过python脚本设置环境变量 [英] Setting environments variables from a python script

查看:86
本文介绍了通过python脚本设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c-shell,并且我习惯使用setenv.我需要从python脚本中执行等效命令.我尝试使用os.environ ['JAVA_HOME'] ="/usr/local/java",它可以从python解释器运行,但是当我的脚本从命令行执行时,运行的shell确实反映了新设置的环境变量.有人可以帮忙吗,我是脚本新手,我希望我把问题弄清楚.

I am using c-shell and I am used to using setenv. I need to execute an equivalent command from within a python script. I tried using os.environ['JAVA_HOME'] = "/usr/local/java" which works from the python interpreter, but when my script is executed from the command line, the shell it ran in does reflect the newly set environment variable. Can anybody help, I am new to scripting, I hope I made my question clear.

推荐答案

如果您使用的是

If you're using subprocess.Popen, it should be enough to pass the env parameter to the constructor to whatever you need as a dictionary (you can copy the contents of os.environ and add your own environment variables if you wish).

这篇关于通过python脚本设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆