Last updated: Apr 10, 2024
Reading timeยท5 min
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.
%env API_KEY="ABC123" %env DB_USER="bobbyhadz"
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.
%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.
%env MAGIC_NUMBER=100 %env MAGIC_NUMBER
Running the code produces the following output.
'100'
Notice that the output is wrapped in quotes.
You can also use the %env var=$val
syntax to set the value of an environment
variable using expansion.
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.
%env
If you need to list all environment variables and values, use the %env
command.
%env
If you need to get the value of an environment variable, use the %env YOUR_VAR
command.
%env API_KEY="ABC123" %env DB_USER="bobbyhadz" %env API_KEY %env DB_USER %env DOES_NOT_EXIT
If you try to get the value of an environment variable that doesn't exist, the following error is shown:
os.environ
You can also use the os.environ mapping object to set and access environment variables in Jupyter Notebook.
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'])
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.
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:
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.
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'))
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.
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:
Name | Description |
---|---|
key | The key for which to return the value |
default | The 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.
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')
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.
!pip install python-dotenv
Alternatively, you can run one of the following commands from your terminal.
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.
API_KEY="ABC123" DB_USER="bobbyhadz" DB_PORT=1234
Now you can import and use the dotenv
module as follows.
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'))
You can learn more about the related topics by checking out the following tutorials:
pd.read_json()