Direnv: Protect sensitive credentials in your project with environment variables

Interested in people contributing to your project? while keeping your sensitive credentials out of the project?

A common practice to prevent sensitive data to be in the source code is to use environment variables.

Let’s see today how it is working with direnv.

Install direnv for Linux

sudo apt-get install direnv

Install direnv for Mac

brew install direnv

Hook direnv with your shell.


Reload your .bashrc file

source ~/.bashrc

Check this link to create a project with environment variables

By going to your folder via the shell, env variables defined in .envrc should now be loaded automatically.

Now time to secure sensitive information in a python project by using env vars.

import os



If the script is executed inside the direnv, we will get ‘foo’ else ‘nope’

python script.py
# foo
cd ../
python direnv-test/script.py


  • https://direnv.net
  • https://direnv.net/docs/installation.html
  • https://freesoft.dev/program/98735739


Laisser un commentaire