Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I declared several variables in my bash (through ~/.bash_profile) , and they work in bash itself as well as in python started in bash.

Example:

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

However, when I start Jupyter notebook, os.environ dont see any of them, showing some default list of variables (PWD, SUDO_USER, USERNAME, JPY_PARENT_PID, SSH_AUTH_SOCK, SUDO_UID, GIT_PAGER etc)

My sistem is Mac OS El Captain, anaconda ipython 2.7

like image 759
Philipp_Kats Avatar asked Oct 08 '15 00:10

Philipp_Kats


2 Answers

Actually this question has nothing to do with Jupyter. It is more related to how different .bash files are loaded.

To quote from 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.

Because you put your variables into .bash_profile it is only available in terminal shells but not in non-interactive shell.

Jupyter-notebook is a web application, even though it runs on your local machine and you access it from local machine (in most cases). Each time you open a notebook it launches a kernel in its own non-interactive shell. So if you want environment variables to be visible from the shell, put them into .bashrc file, not .bash_profile

Also, to view which variables are set, use !export command in your Jupyter notebook

like image 73
Vlad Avatar answered Sep 30 '22 13:09

Vlad


Try env instead of export

eg:

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

Source: link

like image 28
Efren Avatar answered Sep 30 '22 14:09

Efren