Managing Local Environment Variable

Recently I was helping out a colleague set up a local Apache Airflow environment and needed a way of managing various environment variables. We required these to be constrained to the project and not applicable globally to avoid messing up other things he was doing. I remembered using dotenv in the past for this sort of thing, but was looking for something that didn’t require installing nodejs because he didn’t already have it installed.

direnv

Enter direnv, something I can quickly install via the linux package manager.

Nice features:

  • Explicitly opt in each directory ( direnv allow . ), useful when checking out other people’s code
  • Support for .env files like dotenv ( echo dotenv[_if_exists] >> .envrc )
  • Other helpers like PATH_add <path> for easily adding to the PATH variable

Learnings along the way

PYTHONPATH

The environment variable PYTHONPATH can be used to set where local modules can be loaded from. Particularly important with Airflow if you want to use code from outside your dags folder.

bash defaults

Reading the documentation also reminded me of the default syntax in bash which I had forgotten about

$ echo ${FOO-does_not_exist}
does_not_exist

$ export FOO=BAR

$ echo ${FOO-does_not_exist}
BAR