Unable to get environment variables using os python

回眸只為那壹抹淺笑 提交于 2020-07-03 09:33:40

问题


I recently created two environment variables in my terminal as shown below

export SPARK_HOME='/opt/spark/'
export HAIL_HOME='/home/ABCD/.pyenv/versions/3.7.2/envs/bio/lib/python3.7/site-packages/hail/'

When I use echo $SPARK_HOME or echo $HAIL_HOME, I am able to see the path as output

But, when I use the below os commands in jupyter notebook

os.getenv('SPARK_HOME')  # able to get the output /opt/spark/
os.getenv('HAIL_HOME')   # returns no output

I also tried defining the same variables from jupyter as well using os.putenv but even then,I see output only for SPARK_HOME

However, I am able to see in my terminal screen the environment variablesSPARK_HOME and HAIL_HOME using printenv command

Can help me understand what's the problem?


回答1:


I realized that it doesn't produce output for HAIL_HOME because it is installed in my virtual environment. (see .pyenv which is a hidden folder for my virtual environment`)

However, if anyone can confirm this it's even better




回答2:


use os.environ.get("SPARK_HOME").



来源:https://stackoverflow.com/questions/62558582/unable-to-get-environment-variables-using-os-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!