Get

import os
os.environ['USER']
os.environ.get('USER')
# from .env
# pip install python-dotenv
from dotenv import load_dotenv
load_dotenv()
# .env file doesn't need to be in the same folder of the jupyter notebook, for example

Set

In python itself,

os.environ['USER'] = 'New User'

In terminal (we have to retype whenever we use),

export USER='new user'
# check
echo $USER

In .bashrc or .zshrc (we don’t have to retype whenever we use),

export USER='new user'

# Don't forget to "refresh" the current bash
source ~/.zshrc
source ~/.bashrc

Store in a file (Don’t forget to ignore this file from git) → then use python-dotenv.

# .env
USER='new user'