3

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?

Thomas Browne
  • 23,824
  • 32
  • 78
  • 121
  • check the "environment variables section" in [here](https://jupyterhub.readthedocs.io/en/latest/reference/spawners.html) and also check for `jupyter_notebook_config.py` and `jupyterhub_config.py` uses. These files are used as code-over-config style and can be used for customizations. – Yılmaz Durmaz Jan 19 '22 at 23:20
  • you may also want to check https://jupyterhub.readthedocs.io/en/stable/api/spawner.html#jupyterhub.spawner.Spawner.environment – Yılmaz Durmaz Jan 19 '22 at 23:34
  • @YılmazDurmaz please feel free to put in an answer summarizing the docs. – Thomas Browne Jan 20 '22 at 15:18
  • I believe @sai has one important point for the solution. can you please first try that? Please update if you two cant find a fine solution. – Yılmaz Durmaz Jan 20 '22 at 15:36

2 Answers2

3

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

sai
  • 319
  • 1
  • 5
  • Does this allow for independent different user environment variables as per the question? Or does it copy the same server environment variables for all users? – Thomas Browne Jan 20 '22 at 15:19
  • I use a docker notebook/lab instance, not the hub, but it seems they start with some default environment clearing anything from the host if not kept in these `*config.py` files. this answer should solve some problems, if not all. – Yılmaz Durmaz Jan 20 '22 at 15:48
  • As @YılmazDurmaz pointed out, this should solve some of your problems. Is your question similar to what is being pointed out in this thread https://discourse.jupyter.org/t/how-to-access-user-login-shell-environment-variables-from-jupyter-notebook/12183/2 – sai Jan 20 '22 at 19:03
1

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.

Yılmaz Durmaz
  • 2,374
  • 12
  • 26