os.environ.get返回None,但是echo正常工作 [英] os.environ.get return None, but echo works properly

查看:108
本文介绍了os.environ.get返回None,但是echo正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"os.environ.get"命令有问题.我使用以下命令导出了变量:

I have issue with "os.environ.get" command. I exported my variable using:

export MYVAR="123"

然后我用了:

echo "$MYVAR"

它返回了我的价值.但是当我在python中做

And it returned my value. But when i am doing in python

print(os.environ.get('MYVAR'))

它返回None.例如:

It returns None. For example:

print(os.environ.get('HOME'))

正常工作.我尝试使用bashrc,zhrsc,bash_profile,但它永远无法正常工作.我正在使用Linux Elementary 5.1.我该如何解决这个问题?

Works correctly. I tried using bashrc, zhrsc, bash_profile and it never works. I am using Linux Elementary 5.1. How can I solve this problem?

推荐答案

看起来,环境变量的导出仅限于您使用的shell.如果您在系统范围内设置环境变量,则也可以在python代码中读取该变量.

Looks like your export of the environment variable is only limited to the shell you are using. If you set the environment variable system-wide, it will be available to be read in your python code as well.

在/etc/environment中为基于Unix的系统设置环境变量

Set environment variable in /etc/environment for Unix based system

这篇关于os.environ.get返回None,但是echo正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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