python 脚本可以持续更改 Windows 环境变量吗?(优雅地) [英] Can a python script persistently change a Windows environment variable? (elegantly)

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

问题描述

继我的 上一个问题,是否可以制作一个持续更改 Windows 环境变量的 Python 脚本?

一旦 python 解释器终止,对 os.environ 的更改就不会持续存在.如果我在 UNIX 上编写此脚本,我可能会执行以下操作:

set foo=`myscript.py`

但遗憾的是,cmd.exe 没有任何类似于 sh 的反勾号行为的功能.我已经看到了一个非常冗长的解决方案......它确实不是我们可以改进的:

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

微软的头脑肯定有比这更好的解决方案!

注意:与 这个问题.

解决方案

你冗长的解决方案可能是最好的主意;我不相信这可以直接从 Python 中实现.这篇文章提出了另一种方法,使用临时批处理文件:

http://code.activestate.com/recipes/159462/

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

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`

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!

Note: exact duplicate of this question.

解决方案

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://code.activestate.com/recipes/159462/

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

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