jupyterhub / the-littlest-jupyterhub

Simple JupyterHub distribution for 1-100 users on a single server
https://tljh.jupyter.org
BSD 3-Clause "New" or "Revised" License
1.04k stars 340 forks source link

Document how to set environment variables for user notebooks #308

Closed agentS closed 5 years ago

agentS commented 5 years ago

Hi,

First of all, thank you for your great work on this project! It really is a pleasure to work with it.

I am trying to set an environment variable, so I can access it using %env MY_ENVIRONMENT_VARIABLE within Jupyter notebooks. Despite trying for several hours, I have not been able to achieve this simple task.

I tried the following approaches:

I would be most grateful if somebody could tell me the correct way to set an environment variable on the Linux system so that I could use it within my notebooks. Thank you!

yuvipanda commented 5 years ago

Apologies for the delayed response, and thank you for opening this issue.

You can put standard JupyterHub config directives under /opt/tljh/config/jupyterhub_config.d/<some-config-file-name>. So you may create a file at /opt/tljh/config/jupyterhub_config.d/environment.py and add the following contents:

c.Spawner.environment = {
        'TEST': 'blah'
}

This should put the environment variables in your users' notebook servers.

gschintgen commented 5 years ago

Wow, thank you so much for pointing out how to set env variables. I've lost hours over it, too.

For the record and for future reference: sagemath (called "sage" in conda) requires two environment variables. Putting the following in /opt/tljh/config/jupyterhub_config.d/environment.py makes sage stop complaining:

c.Spawner.environment = {
        'SAGE_ROOT': '/opt/tljh/user',
        'SAGE_LOCAL': '/opt/tljh/user'
}

Well, sage is still not working, due to some very troublesome prompt_toolkit related breakage that I'm trying to figure out, but at least I'm that one step closer. (It's somewhat frustrating.)

filippo82 commented 5 years ago

Dear @yuvipanda, this solution does not work for me. As mentioned above, I've created the /opt/tljh/config/jupyterhub_config.d/environment.py file and added only this content:

c.Spawner.environment = {
        'TEST'_VAR: 2,
}

However, after logging in with one of the TLJH users, I do not see the TEST_VAR using os.environ. Is there anything I am missing? Do I have to restart TLJH? Do I need to recreate delete and recreate the user? Is the c.Spawner.environment command not correct?

Thank you.

yuvipanda commented 5 years ago

Hello @filippo82!

You should restart TLJH after adding it - the config is only read on startup. After that, you need to stop & start the user server again. Try that out and see if that works?

filippo82 commented 5 years ago

Hi @yuvipanda, thanks! Yes,

tljh-config reload
tljh-config reload proxy

did the trick!

Since I have your attention ... :) I would also need to source a file to set some variables, like this one to set up the Intel compiler:

source $HOME/longpath/psxevars.sh intel64

Is there a way to do that within TLJH?

Thank you.

yuvipanda commented 5 years ago

@filippo82 unfortunately there isn't a clean way to do that - source is from bash, while this all is in Python. You might be able to write some python code that parses it (or even executes it!) and pass those on.

Also you probably don't need the reload proxy

filippo82 commented 5 years ago

OK, thanks!

ksbek commented 4 years ago

Apologies for the delayed response, and thank you for opening this issue.

You can put standard JupyterHub config directives under /opt/tljh/config/jupyterhub_config.d/<some-config-file-name>. So you may create a file at /opt/tljh/config/jupyterhub_config.d/environment.py and add the following contents:

c.Spawner.environment = {
        'TEST': 'blah'
}

This should put the environment variables in your users' notebook servers.

@yuvipanda I want to set env vars per user. How can I do that? Thanks in advance.

kedare commented 4 years ago

Is this also the way to update the PATH variable ? I would like to add a new path in the PATH variable without overriding, how can I do this using this method ? (Or there is another way to do so)?

Related to https://github.com/google/evcxr/issues/140

CagtayFabry commented 4 years ago

Is this also the way to update the PATH variable ? I would like to add a new path in the PATH variable without overriding, how can I do this using this method ? (Or there is another way to do so)?

Related to google/evcxr#140

I tend to add multi-user PATH variables via c.Spawner.environment as well as /etc/environment (ubuntu 18.04) to make sure it shows up in kernels and terminals (disclaimer: I'm only working with python kernels)

meeseeksmachine commented 3 years ago

This issue has been mentioned on Jupyter Community Forum. There might be relevant details there:

https://discourse.jupyter.org/t/tljh-how-to-set-env-vars-for-users-after-but-not-for-the-spawner/6725/1

chance2021 commented 3 years ago

I managed to modify PATH as below configuration in values.yaml

hub:
  extraConfig: |
  def notebook_dir_hook(spawner):
    spawner.environment = {'PATH': '/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/shared'}
meeseeksmachine commented 2 years ago

This issue has been mentioned on Jupyter Community Forum. There might be relevant details there:

https://discourse.jupyter.org/t/how-to-access-user-login-shell-environment-variables-from-jupyter-notebook/12183/2

Sarrouna commented 2 years ago

sorry for this comment because the issue is already closed, but really I need your help for my issue:

https://github.com/jupyterhub/the-littlest-jupyterhub/issues/805 @yuvipanda please help Thanks a lot for your understanding

safay commented 1 year ago

I'm having an issue where I update the /opt/tljh/config/jupyterhub_config.d/environment.py file but the environment variables are not updating. I have executed sudo tljh-config reload and sudo tljh-config reload proxy, logged out and back in with my user, and shut down and restarted the notebook kernels, but the new environment config present in environment.py is not accessible to a call to os.environ in the notebooks. What could cause this?

edit: The only thing that would work to refresh the environment variables was to restart the instance (we're running on AWS EC2).

mineshaftgap commented 1 year ago

So I really wanted environmental variables on a per user instead of global (/opt/tljh/config/jupyterhub_config.d/environment.py) level. The way that I found to do it per user is in a file /home/jupyter-/.ipython/profile_default/startup/00-env.py like so:

import os

os.environ["VAR1"] = "FOO"
os.environ["VAR2"] = "BAR"
NickDubelman commented 4 months ago

Hopefully this will help someone else-- after making a change to the config, I kept reloading the tljh config, as well as the user's kernel, but the change would not take affect. Turns out you need to restart the user's Jupyter "server". As in:

image

Edit: To clarify, seems like the minimal required steps after updating the /opt/tljh/config/jupyterhub_config.d/environment.py are:

  1. tljh-config reload
  2. Stop then start the Jupyter "server"