Jupyter Notebook
Environment Variables
Python
Data Science
Programming Tips

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.

python
1import os
2
3os.environ["DATA_DIR"] = "/tmp/project-data"
4os.environ["APP_MODE"] = "development"
5
6print(os.environ["DATA_DIR"])

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.

python
1import os
2import subprocess
3
4os.environ["GREETING"] = "hello"
5subprocess.run(["python", "-c", "import os; print(os.environ['GREETING'])"], check=True)

Use %env for Quick Notebook Work

IPython provides the %env magic, which is convenient for quick interactive work.

python
%env DATA_DIR=/tmp/project-data
%env APP_MODE=development

You can also inspect values:

python
%env DATA_DIR

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.

python
!export TOKEN=abc123
!echo $TOKEN

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.

python
1import os
2
3api_key = os.getenv("API_KEY")
4if api_key is None:
5    print("API_KEY is not set")
6else:
7    print("API key loaded")

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:

bash
export DATA_DIR=/tmp/project-data
jupyter lab

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.

python
1import os
2
3required = ["API_KEY", "DB_HOST"]
4missing = [name for name in required if not os.getenv(name)]
5
6if missing:
7    print("Missing variables:", ", ".join(missing))
8else:
9    print("Environment looks complete")

When .env Files Are More Practical

For local workflows, a .env file plus a loader library can be easier than typing variables repeatedly.

python
1from dotenv import load_dotenv
2import os
3
4load_dotenv()
5print(os.getenv("DATA_DIR"))

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.environ to set variables for the current Python kernel.
  • '%env is convenient for interactive notebook work.'
  • '!export usually 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.

Course illustration
Course illustration

All Rights Reserved.