Calmcode - env variables: values

Loading values

1 2 3 4 5 6

This is the "before" situation.

import os
from dotenv import load_dotenv

load_dotenv(override=True)

print(len(os.environ))
print(os.environ["VIRTUAL_ENV"])
print(os.environ["FOOBAR"])
print(os.environ["HELLO"])

Notice that we are adding all the environment variables to the os.environ dictionary. This is not always what you want because you might loose track of the trees because of the forest. Instead you can also choose to load the values directly into a dictionary.

import os
from dotenv import dotenv_values

config = dotenv_values(".env")
print(config)

This way you only have to consider a config dictionary that only has the variables defined in the .env file. It won't have all the other environment variables that are already set in your system.