Search code examples
jupyter-notebookenvironment-variablesjupyterjupyter-labjupyterhub

Setting environment variables in jupyter hub


I followed approach in this thread. I can easily set env variable in jupyter hub using the %env VAR = 5. However, when I try to print out this variable in the terminal I get only a blank line, as if the variable did not exist at all. Is it somehow possible to be able to print in terminal the env var defined in the notebook?


Solution

  • Setting environment variables from the notebook results in these variables being available only from that notebook.

    %env VAR=TEST
    import os
    print(os.environ["VAR"])
    ...
    >>> TEST
    

    If you want to persist the variable, you need to put it either in the kernel.json file, or in systemd service file for jupyterhub, or in something like ~/.bashrc.