可python脚本坚持更改Windows环境变量? (高雅) [英] Can a python script persistently change a Windows environment variable? (elegantly)

查看:150
本文介绍了可python脚本坚持更改Windows环境变量? (高雅)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的<一继href=\"http://stackoverflow.com/questions/488294/how-can-i-make-a-windows-batch-file-which-changes-an-environment-variable\">$p$pvious问题,是有可能使一个Python脚本坚持更改了Windows环境变量?

Following on from my previous question, is it possible to make a Python script which persistently changes a Windows environment variable?

更改os.environ一旦蟒蛇间preTER终止不会持续存在。如果我是在UNIX脚本这一点,我可能会做这样的事情:

Changes to os.environ do not persist once the python interpreter terminates. If I were scripting this on UNIX, I might do something like:

set foo=`myscript.py`

但是,唉,CMD.EXE没有任何就像SH的后台剔行为。我看到一个非常冗长的解决方案...这AINT pretty所以我们肯定可以改善这一点:

But alas, cmd.exe does not have anything that works like sh's back-tick behavior. I have seen a very long-winded solution... it 'aint pretty so surely we can improve on this:

for /f "tokens=1* delims=" %%a in ('python  ..\myscript.py') do set path=%path%;%%a

当然,在微软心目中有比这更好的解决办法!

Surely the minds at Microsoft have a better solution than this!

注意:精确复制的<一个href=\"http://stackoverflow.com/questions/488366/how-do-i-make-environment-variable-changes-stick-in-python\">this问题。

推荐答案

您啰嗦的解决方案可能是最好的办法;我不相信这是可能在Python直接。本文提出的另一种方法,使用临时批处理文件:

Your long-winded solution is probably the best idea; I don't believe this is possible from Python directly. This article suggests another way, using a temporary batch file:

的http://$c$c.activestate.com/recipes/159462/

这篇关于可python脚本坚持更改Windows环境变量? (高雅)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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