Calmcode - env variables: environment variables

Environment variables from Python

1 2 3 4 5 6

You can define environment variables directly from the terminal. This is useful if you want to set a variable that is only used in a single terminal session.

export FOOBAR=1

This will set the variable FOOBAR to 1. You can access this variable from Python using the os module.

import os

print(os.environ["FOOBAR"])

But notice, os.environ is a dictionary. This means that there are all sorts of other variables in there as well. Feel free to have a look yourself.

Sessions

If you start a new terminal and don't set the environment variable then you'll notice that the FOOBAR key is missing. This is because the environment variables are session specific. If we want these environment variables to always be available we need to set them in a more permanent way.