英文:
Environment variables unavailable to jupyter notebook but I see them in base printenv
问题
我在激活脚本中添加了几个环境变量,方法是在最后添加以下内容:
export MYVAR=blahblah
当我通过 source ./activate
激活后,然后执行 printenv
命令,我可以看到它们。
从 bash 终端中,如果我启动 Python,然后执行以下代码:
import os
os.environ
那么我也可以看到它们。
但是,如果我通过 Web 接口或者通过 VSC 远程连接来打开 Jupyter 笔记本,我就看不到它们了。
缺少了什么链接?
英文:
I added a few environment variables to my activate script by adding
export MYVAR=blahblah
to the end.
When I activate via source ./activate
and then do printenv
I see them.
From a bash terminal if I launch python then do
import os
os.environ
then I see them.
BUT if I open a jupyter notebook either through the web interface or through VSC remote connection then I don't see them.
What's the missing link?
答案1
得分: 0
以下是翻译好的部分:
我不确定为什么在激活脚本中设置的环境变量不能在笔记本会话中保留,但我发现使用内核启动可以满足我的需求。
具体来说,我使用了 dot-env 和 ipython 内核启动文件夹。
我在 ~/.ipython/profile_default/startup
文件夹中创建了一个两行脚本,内容如下:
fromdotenv import load_dotenv
load_dotenv()
并将其保存为 00_make_envs.py
。
此外,我在相同的文件夹中创建了一个 .env
文件,其中包含我想要使用的所有环境变量,格式与 Docker 使用的格式相同,例如:
MYVAR1=blahblah
MYVAR2=foobar
英文:
I'm not sure why the environment variables set in the activate script don't persist to a notebook session but I found that using kernel startups does what I need.
Specifically I used dot-env and the ipython kernel startup folder.
I made a 2 line script in ~/.ipython/profile_default/startup
which just says
fromdotenv import load_dotenv
load_dotenv()
and saved it as 00_make_envs.py in that folder
Also, I made an .env
file in that same folder that has all the environment variables I want to use, it's in the same format that docker uses... ie
MYVAR1=blahblah
MYVAR2=foobar
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论