Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to set environment variables for user notebooks #308

Closed
agentS opened this issue Apr 24, 2019 · 17 comments
Closed

Document how to set environment variables for user notebooks #308

agentS opened this issue Apr 24, 2019 · 17 comments
Labels
needs-documentation Issue that can be solved with documentation

Comments

@agentS
Copy link

agentS commented Apr 24, 2019

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:

  • setting it in .bashrc
  • setting it in .profile
  • setting it in the SystemD configuration file
  • adding a new configuration file in the directory /opt/tljh/config/jupyterhub_config.d
  • setting it via kernel parameters (see this StackOverflow answer)
  • setting it in the spawner configuration
  • setting it via sudo tljh-config set user_environment.MY_ENVIRONMENT_VARIABLE my_value
  • adding the environment variable name to the list of forwarded environment variables

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
Copy link
Collaborator

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 yuvipanda changed the title Set environment variable Document how to set environment variables for user notebooks May 20, 2019
@yuvipanda yuvipanda added the needs-documentation Issue that can be solved with documentation label May 20, 2019
@gschintgen
Copy link

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.)

@agentS agentS closed this as completed Jun 18, 2019
@filippo82
Copy link

filippo82 commented Oct 8, 2019

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
Copy link
Collaborator

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
Copy link

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
Copy link
Collaborator

@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
Copy link

OK, thanks!

@ksbek
Copy link

ksbek commented Nov 25, 2019

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
Copy link

kedare commented Nov 4, 2020

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 evcxr/evcxr#140

@CagtayFabry
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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

#805
@yuvipanda please help
Thanks a lot for your understanding

@safay
Copy link

safay commented Feb 16, 2023

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
Copy link

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
Copy link

NickDubelman commented Jun 26, 2024

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-documentation Issue that can be solved with documentation
Projects
None yet
Development

No branches or pull requests