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

2024-09-27 00:21:36 发布

您现在位置:Python中文网/ 问答频道 /正文

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

示例:

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

但是,当我启动Jupyter notebook时,os.environ看不到它们,显示了一些默认的变量列表(PWD、SUDO\u USER、USERNAME、JPY_PARENT_PID、SSH_AUTH_SOCK、SUDO UID、GIT_PAGER等)

我的妹妹是Mac OS El船长,anaconda ipython 2.7


Tags: bash声明示例ossudojupyterexportprofile
2条回答

尝试env而不是export

例如:

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

来源:link

事实上这个问题和朱庇特无关。它与如何加载不同的.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是一个web应用程序,即使它运行在您的本地计算机上并且您可以从本地计算机访问它(大多数情况下)。每次打开笔记本电脑,它都会在自己的非交互式shell中启动内核。因此,如果希望环境变量在shell中可见,请将它们放入.bashrc文件中,而不是.bash_profile

另外,要查看设置了哪些变量,请使用Jupyter笔记本中的!export命令

相关问题 更多 >

    热门问题