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.
direnv for Linux
sudo apt-get install direnv
direnv for Mac
brew install direnv
Hook direnv with your shell.
Reload your .bashrc file
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 POSTGRESQL_PASSWORD = os.getenv('FOO') print(POSTGRESQL_PASSWORD)
If the script is executed inside the direnv, we will get ‘foo’ else ‘nope’
python script.py # foo cd ../ python direnv-test/script.py None