[英]How to access system environment variables using Python
I am aware that you can use os.environ['key']
to access an environment variable, but it appears that this function is localized to environment variables for the signed in user. 我知道您可以使用os.environ['key']
访问环境变量,但是看来此功能已本地化为已登录用户的环境变量。 I need to access the system environment variables. 我需要访问系统环境变量。 Is this possible? 这可能吗?
You cannot do this using os.environ
in python. 您无法在python中使用os.environ
来执行此操作。 You can, however, use os.system
to run a commands such as: 但是,您可以使用os.system
运行以下命令:
sudo -Hiu $user env | grep $var
to access a variable from another user. 从另一个用户访问变量。 This is a bit hacky, but it works. 这有点棘手,但可以。 And your options are limited here. 并且您的选择在这里受到限制。
Also -- why do you want to? 另外-为什么要这么做? You should have a process for setting the environment variables before this script is run at all, ideally. 理想情况下,应该在此脚本完全运行之前,有一个设置环境变量的过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.