gpt4 book ai didi

python os.getenv() 在 Jupyter notebook (os x el capitan) 中看不到我的任何自定义变量

转载 作者:太空狗 更新时间:2023-10-30 00:16:55 29 4
gpt4 key购买 nike

我在我的 bash 中声明了几个变量(通过 ~/.bash_profile),它们在 bash 本身以及在 bash 中启动的 python 中工作。

例子:

export PUIBy="Dropbox/CUSP/1_1_PUI/PUI_Bycicle_Research/"

但是,当我启动 Jupyter notebook 时,os.environ 看不到它们中的任何一个,显示一些默认变量列表(PWD、SUDO_USER、USERNAME、JPY_PARENT_PID、SSH_AUTH_SOCK、SUDO_UID、GIT_PAGER 等)

我的系统是Mac OS El Captain, anaconda ipython 2.7

最佳答案

其实这个问题和Jupyter无关。它与不同的 .bash 文件的加载方式更相关。

引自here

.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile is executed to configure your shell before the initial command prompt.

因为您将变量放入 .bash_profile 中,所以它仅在终端 shell 中可用,但在非交互式 shell 中不可用。

Jupyter-notebook 是一个网络应用程序,即使它在您的本地计算机上运行并且您可以从本地计算机访问它(在大多数情况下)。每次打开笔记本时,它都会在自己的非交互式 shell 中启动内核。因此,如果您希望环境变量在 shell 中可见,请将它们放入 .bashrc 文件,而不是 .bash_profile

此外,要查看设置了哪些变量,请在 Jupyter notebook 中使用 !export 命令

关于python os.getenv() 在 Jupyter notebook (os x el capitan) 中看不到我的任何自定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33004825/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com