简体   繁体   English

os.environ 不显示 Jupyter notebook 中的所有环境变量

[英]os.environ doesn't show all environmental variables in Jupyter notebook

I am finding that the following code prints out my expected environmental variable when I execute the script in a shell:我发现当我在 shell 中执行脚本时,以下代码会打印出我预期的环境变量:

import os
print(os.environ['STUFF'])

However, when I run this same code in Jupyter Notebook, I get a key error.但是,当我在 Jupyter Notebook 中运行相同的代码时,出现关键错误。 I have tried restarting the Jupyter server.我尝试重新启动 Jupyter 服务器。 What else should I do?我还应该做什么?

You can query all the dictionary keys to confirm the one you have is not there.您可以查询所有字典键以确认您拥有的键不存在。

import os
print(os.environ.keys())

Or better you can test for the presence of the specific key.或者更好的是您可以测试特定键的存在。

print(os.environ.has_key('STUFF'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM