How to set and get Environment Variables in Jupyter Notebook

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
5 min

banner

# Table of Contents

  1. Set and get environment variables in Jupyter Notebook using magic commands
  2. Setting a value of an environment variable using expansion
  3. List all environment variables using %env
  4. Get the value of an environment variable
  5. Setting and accessing environment variables in Jupyter Notebook with os.environ
  6. Using the python-dotenv package to set environment variables in Jupyter Notebook

# Set and get environment variables in Jupyter Notebook using magic commands

You can use magic commands to set and get environment variables in Jupyter Notebook, e.g. %env API_KEY="ABC123".

The %env magic command is used to get, set or list environment variables.

main.py
%env API_KEY="ABC123" %env DB_USER="bobbyhadz"

set environment variables using magic command

Magic commands start with % when using the IPython kernel.

The %env command is used to get, set and list environment variables.

You might also see the following syntax used to set environment variables.

main.py
%env API_KEY "ABC123" %env DB_USER "bobbyhadz"

Separating the key and value with a space or an equal sign works in the same way.

The code sample above shows how to set the API_KEY and DB_USER environment variables.

Note that environment variables always store strings, so even if you set an environment variable to an integer, it would automatically get converted to a string.

main.py
%env MAGIC_NUMBER=100 %env MAGIC_NUMBER

Running the code produces the following output.

shell
'100'

Notice that the output is wrapped in quotes.

# Setting a value of an environment variable using expansion

You can also use the %env var=$val syntax to set the value of an environment variable using expansion.

main.py
name = "bobby" + "hadz" %env API_KEY="ABC123" %env DB_USER=$name

The code sample replaces the $name expression with the actual value of the name Python variable.

set environment variable using expansion

# List all environment variables using %env

If you need to list all environment variables and values, use the %env command.

main.py
%env

list all environment variables

# Get the value of an environment variable

If you need to get the value of an environment variable, use the %env YOUR_VAR command.

main.py
%env API_KEY="ABC123" %env DB_USER="bobbyhadz" %env API_KEY %env DB_USER %env DOES_NOT_EXIT

get values of environment variables

If you try to get the value of an environment variable that doesn't exist, the following error is shown:

  • UsageError: Environment does not have key: DOES_NOT_EXIT

# Setting and accessing environment variables in Jupyter Notebook with os.environ

You can also use the os.environ mapping object to set and access environment variables in Jupyter Notebook.

main.py
import os # Set environment variables os.environ['API_KEY'] = "ABC123" os.environ['DB_USER'] = "bobbyhadz" # Access environment variables print(os.environ['API_KEY']) print(os.environ['DB_USER'])

set and access environment variables using os environ

The first two lines set the API_KEY and DB_USER environment variables.

The second two lines print their values.

Make sure to import the os module, otherwise, you'd get a NameError.

Note that environment variables must be strings.

If you have a variable that stores an integer, use the str class to convert it to a string.

main.py
import os os.environ['API_KEY'] = "ABC123" os.environ['DB_USER'] = "bobbyhadz" # ๐Ÿ‘‡๏ธ Convert the value to String before setting ENV VAR os.environ['MAGIC_NUMBER'] = str(100) print(os.environ['API_KEY']) print(os.environ['DB_USER']) print(os.environ['MAGIC_NUMBER'])

Output:

shell
ABC123 bobbyhadz 100

When using square brackets to access environment variables, you would get a KeyError exception if the environment variable is not set.

You can use the dict.get method to return a default value instead of raising an error.

main.py
import os os.environ['API_KEY'] = "ABC123" os.environ['DB_USER'] = "bobbyhadz" print(os.environ.get('API_KEY', 'default value')) print(os.environ.get('DB_USER', 'default value')) print(os.environ.get('DOES_NOT_EXIST', 'default value'))

return default value if environment variable not set

The third environment variable is not set, so the supplied default value is returned.

If you simply want to return None if the environment variable is not set, only pass a single argument to the dict.get() method.

main.py
import os print(os.environ.get('DOES_NOT_EXIST')) # ๐Ÿ‘‰๏ธ None

The dict.get() method returns the value for the given key if the key is in the dictionary, otherwise a default value is returned.

The method takes the following 2 parameters:

NameDescription
keyThe key for which to return the value
defaultThe default value to be returned if the provided key is not present in the dictionary (optional)

If a value for the default parameter is not provided, it defaults to None, so the get() method never raises a KeyError.

If you need to check if an environment variable is set, use the in operator.

main.py
import os os.environ['API_KEY'] = "ABC123" os.environ['DB_USER'] = "bobbyhadz" if 'API_KEY' in os.environ: # ๐Ÿ‘‡๏ธ This runs print('The API_KEY environment variable exists') else: print('The API_KEY environment variable does NOT exist')

# Using the python-dotenv package to set environment variables in Jupyter Notebook

You can also use the python-dotenv module to set environment variables in Jupyter Notebook.

Run the following command in a cell to install the module.

shell
!pip install python-dotenv

Alternatively, you can run one of the following commands from your terminal.

shell
pip install python-dotenv pip3 install python-dotenv

Now create a .env file in the root directory of your project and set some environment variables.

.env
API_KEY="ABC123" DB_USER="bobbyhadz" DB_PORT=1234

create dotenv file in root directory of project

Now you can import and use the dotenv module as follows.

main.py
import os from dotenv import load_dotenv # Load the environment variables from .env load_dotenv() # Access the environment variables print(os.environ.get('API_KEY')) print(os.environ.get('DB_USER')) print(os.environ.get('DB_PORT'))

using dotenv to set environment variables

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev