如何使环境变量变为Python? [英] How do I make environment variable changes stick in Python?

查看:111
本文介绍了如何使环境变量变为Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我读到的内容,Python实例中的环境变量的任何更改仅在该实例中可用,并且在实例关闭后就会消失。有没有办法让他们坚持使用它们进入系统?

From what I've read, any changes to the environment variables in a Python instance are only available within that instance, and disappear once the instance is closed. Is there any way to make them stick by committing them to the system?

我需要做的这一点是因为在我工作的工作室,像Maya这样的工具依赖很重要的环境变量来配置跨多个平台的路径。

The reason I need to do this is because at the studio where I work, tools like Maya rely heavily on environment variables to configure paths across multiple platforms.

我的测试代码是

import os
os.environ['FAKE'] = 'C:\\'

打开另一个Python实例,并请求 os.environ ['FAKE'] 产生一个 KeyError

Opening another instance of Python and requesting os.environ['FAKE'] yields a KeyError.

注意:可移植性将是一个问题,但我正在编写的小型API将能够检查操作系统版本并触发不同的命令如果需要,

NOTE: Portability will be an issue, but the small API I'm writing will be able to check OS version and trigger different commands if necessary.

说,我已经走了使用Windows注册表技术的路线,并将简单地编写另外的方法,在其他平台上调用shell脚本,

That said, I've gone the route of using the Windows registry technique and will simply write alternative methods that will call shell scripts on other platforms as they become requirements.

推荐答案

在Windows下可以您可以通过这个食谱通过注册表对环境变量进行更改,尽管它似乎像过度杀伤一样。

Under Windows it's possible for you to make changes to environment variables persistent via the registry with this recipe, though it seems like overkill.

要回应Brian的问题,你想完成什么?这可能是一个更简单的方法。

To echo Brian's question, what are you trying to accomplish? There is probably an easier way.

这篇关于如何使环境变量变为Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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