How to set env variable in Jupyter notebook
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Environment variables are useful in notebooks because they let you keep configuration outside the code cell itself. They are commonly used for API keys, file locations, feature flags, and tool settings, but the exact way you set them affects how long they last and which processes can read them.
Set Variables Inside Python with os.environ
If you want the current notebook kernel to see a variable, set it with os.environ.
This updates the environment for the running Python process. Any Python code in later cells can read those values, and child processes started from that kernel usually inherit them.
Use %env for Quick Notebook Work
IPython provides the %env magic, which is convenient for quick interactive work.
You can also inspect values:
This is handy when you want notebook-oriented syntax, but it still affects only the current kernel session unless you set the value somewhere more permanent.
Understand the Difference Between Python State and Shell State
A common source of confusion is using shell commands with ! and expecting their environment changes to persist.
That usually does not work the way people expect because each ! command runs in a separate subprocess. The export happens in one shell, then disappears when that shell exits.
If you want a value available to Python code, set it in Python or with %env, not with a standalone !export command.
Read Variables Safely
Environment variables may be missing, especially when a notebook is shared with teammates or executed on CI infrastructure. Prefer defensive reads.
That is better than indexing os.environ directly when the variable is optional.
Persist Variables Across Notebook Sessions
If you need a value every time Jupyter starts, set it outside the notebook.
Common options include:
- exporting it before launching Jupyter
- placing it in a shell startup file for local development
- configuring it in the environment where JupyterHub, Docker, or your cloud notebook service runs
For example, from a terminal:
The notebook kernel inherits that environment when it starts.
Keep Secrets Out of Notebook Output
Secrets stored in environment variables are still visible to the running process, so they are not magically safe. Avoid printing them in notebook output, and avoid committing notebook cells that reveal them.
A simple pattern is to validate presence without echoing the actual value.
When .env Files Are More Practical
For local workflows, a .env file plus a loader library can be easier than typing variables repeatedly.
That works well when the notebook is part of a larger Python project that already uses .env configuration.
Common Pitfalls
The biggest mistake is assuming !export changes the notebook kernel environment permanently. Another is hardcoding secrets directly into cells instead of loading them from the environment. Developers also sometimes forget that variables set inside the notebook disappear when the kernel restarts. Finally, printing secret values for debugging can leak them into saved notebook output.
Summary
- Use
os.environto set variables for the current Python kernel. - '
%envis convenient for interactive notebook work.' - '
!exportusually does not persist across notebook shell commands.' - Use
os.getenv()when a variable may be missing. - Set variables outside Jupyter if they must persist across kernel restarts.

