skip to Main Content

JupyterHub has various authentication methods, and the one I am using is the PAMAuthenticator, which basically means you log into the JupyterHub with your Linux userid and password.

However, environment variables that I create, like this (or for that matter in those set in my .bashrc), before running JupyterHub, do not get set within the user’s JupyterLab session. As you can see they’re available in the console, with or without the pipenv, and within python itself via os.getenv().

enter image description here

However in JupyterHub’s spawned JupyterLab for my user (me):

enter image description here
This environment variable myname is not available even if I export it in a bash session from within JupyterLab as follows:

enter image description here

Now the documentation says I can customize user environments using a Docker container for each user, but this seems unnecessarily heavyweight. Is there an easier way of doing this?

If not, what is the easiest way to do this via Docker?

2

Answers


  1. In the jupyterhub_config.py file, you may want to add the environment variables which you need using the c.Spawner.env_keep variable

    c.Spawner.env_keep = ['PATH', 'PYTHONPATH', 'CONDA_ROOT', 'CONDA_DEFAULT_ENV', 'VIRTUAL_ENV', 'LANG', 'LC_ALL', 'JUPYTERHUB_SINGLEUSER_APP']
    

    Additional information on all the different configurations are available at https://jupyterhub.readthedocs.io/en/stable/reference/config-reference.html

    Login or Signup to reply.
  2. Unfortunately, unlike a single-user Jupyter notebook/lab, Jupyterhub is for a multi-user environment and the customization along with setting security is not some concrete area. They provide you some default settings and a ton of ways to customize the use, alas they provide only a handful amount of examples. You need to dig into documents, check for similarities to your use case, and make adjustments in a trial-error process.

    Fortunately, other than using configuration files used to configure Jupyterhub and Jupyter notebook servers, namely jupyter_notebook_config.py and jupyterhub_config.py, we can use environment reading packages per user. This flexibility comes from the use of a programming language kernel.

    But this needs being able to install new packages, having them already installed, or asking admins to install them on the current kernel.

    Here is one way to use customized environment variables in the current workspace.

    • Create a new file and give a clear name to show it is an environment file. You can have as many different files as you need. Most production exercises use the .env name but jupyter will not list dot files in file view so avoid doing that. Also, be careful about quotes; sometimes you need them, sometimes you get errors depending on what library you use and where you use them.

    test.env:

    NAME="My Name"
    TEST=This is test 42
    
    • Install and use your preferred environment file reader then read from the file(s) you want. you can use `pip install“ in the notebook when needed, just use it cautiously.

    test.ipynb

    #package already installed, so installation commented out
    #%pip install python-environ
    import environ
    env = environ.Env()
    env.read_env(env.str('ENV_PATH', 'test.env'))
    NAME=env("NAME")
    TEST=env("TEST")
    print(NAME," : ",TEST)
    

    If you are an admin of the hub, then beware of the use cases for libraries such that some may break your restrictions. So keep an eye on what permissions you give to your users. If you use custom docker images though, there should not be a leakage as they are already designed to be isolated from your system.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search