Thi's avatar
HomeAboutNotesBlogTopicsToolsReading
About|My sketches |Cooking |Cafe icon Support Thi
πŸ’Œ [email protected]

Environment Variables in Python

Anh-Thi Dinh
Python
Left aside

Get

Set

In python itself,
In terminal (we have to retype whenever we use),
In .bashrc or .zshrc (we don’t have to retype whenever we use),
Store in a file (Don’t forget to ignore this file from git) β†’ then use python-dotenv.
Β 
About|My sketches |Cooking |Cafe icon Support Thi
πŸ’Œ [email protected]
1import os
2os.environ['USER']
3os.environ.get('USER')
1# from .env
2# pip install python-dotenv
3from dotenv import load_dotenv
4load_dotenv()
5# .env file doesn't need to be in the same folder of the jupyter notebook, for example
1os.environ['USER'] = 'New User'
1export USER='new user'
2# check
3echo $USER
1export USER='new user'
2
3# Don't forget to "refresh" the current bash
4source ~/.zshrc
5source ~/.bashrc
1# .env
2USER='new user'