If you want to get and set environment variables using Python, simply use the relevant methods from os
. To set an environment variable, do this:
import os
'SOME_ENV_VARIABLE'] = 13.5 os.environ[
And to access an environment variable, there are actually a number of different ways. All these three are essentially the same:
'SOME_ENV_VARIABLE')
os.getenv('SOME_ENV_VARIABLE')
os.environ.get('SOME_ENV_VARIABLE') os.environ(
For the final one (os.environ('SOME_ENV_VARIABLE')
), if the variable doesn’t exist, it’ll return a KeyError
, whereas the first two will just return None
in that case.