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

python os.getenv() don't see any of my custom variables from Jupyter notebook (os x el capitan)

我在 bash 中声明了几个变量(通过 ~/.bash_profile),它们在 bash 本身以及 python 开始于 bash.

示例:

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

尝试 env 而不是 export

例如:

env PUIBy="Dropbox/CUSP/1_1_PUI/PUI_Bycicle_Research/" jupyter notebook

来源:link

其实这个问题和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 笔记本中使用 !export 命令