written by Eric J. Ma on 2021-05-14 | tags: til jupyter data science
Today, I learned that one can set an environment variable from within a Jupyter notebook session.
Let's say you needed an environment variable set for your Jupyter notebook, but you:
To do so, add the following line(s) to the very, very first cell of the notebook:
%env MY_ENV_VAR=value
Within the same code cell, you can clone that as many times as you want to set environment variables of any kind.
To verify that the environment variable has been set correctly, you can use the following code block to view the value of the environment variable that you set:
import os os.environ["MY_ENV_VAR"]
Now, one thing has to be stated - according to 12 Factor App development principles, you might need to set environment variables for sensitive information. You should never, ever, ever set those environment variables at the top of your notebook because they will be exposed!
I send out a newsletter with tips and tools for data scientists. Come check it out at Substack.
If you would like to receive deeper, in-depth content as an early subscriber, come support me on Patreon!