Python 3 read and set environmental variable

If you want to read, set and modify environmental variables with python you can do it by:

  • reading
import os
myenv = os.environ['MYENV']
  • reading with method
import os
os.environ.get('MYENV')
  • reading with default
import os
os.environ.get('MYENV', 'None')
  • setting/modifying
import os
os.environ["MYENV"] = "MyValue"
  • setting/modifying integer value
import os
os.environ["MYENV"] = "12"

Related Article