5

I set up a systemctl service (Jupyter) as part of a bootstrap for a Hadoop cluster. After bootstrap actions are completed, Hadoop is installed. During that step a number of environment variables are set - and these are vital for my service, Jupyter. Problem is, Jupyter does not know these environment variables, as it is started earlier in the process.

My question: how can I reload environment variables for my user? At the moment the user is running Jupyter, all env variables are known - I just need them loaded into the Python interpreter.

This fails inside notebook: os.environ['JAVA_HOME'] but works fine in the interpreter (because JAVA_HOME was defined after Jupyter was started).

3
  • 1
    environment variables are local to a process when it starts and inherited from the parent process I think you are not going to be able to set them from outside that process. You can set them in that process but you would need to establish some way to pass them in. (example put in file and have something on a thread periodically read and set them local to that process) Commented Jun 22, 2020 at 12:53
  • @LhasaDad Thanks for explaining, that's what I eventually did. I will be happy to accept that as an answer. Commented Jun 22, 2020 at 17:59
  • Thanks, added it as an answer @Lukasz Commented Jun 22, 2020 at 18:05

2 Answers 2

4

Environment variables are local to a process when it starts and inherited from the parent process I think you are not going to be able to set them from outside that process. You can set them in that process but you would need to establish some way to pass them in. (example put in file and have something on a thread periodically read and set them local to that process)

Sign up to request clarification or add additional context in comments.

4 Comments

Hi, LhasaDad, would you please give more details or steps how do you set the environment variable in Jupyter notebook? Thanks
os.environ['JAVA_HOME'] ="path" should work that is setting the value will set it within the running process.
Ok, I see, that works for me. But just curious what is your working flow to load all your environment variables? Do you put those lines to load environment variables i a file or what? Thanks
i was going on general knowledge of processes and environment inheritance. I don't do a lot with Jupyter notebooks. It was more from general Python programming that I provided my response.
-1

Python 3.14 should bring os.reload_environ to deal with changed environment.

1 Comment

I don't think this would work. As it's noted in the PR and discussion linked in the PR, it applies to the same process. In my use cases we'd need out-of-process refresh, which, to my understanding, is not what the PR delivers (in fact a number of people noted that feature could be confusing).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.