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.
https://direnv.net/docs/hook.html
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
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
Sources
- https://direnv.net
- https://direnv.net/docs/installation.html
- https://freesoft.dev/program/98735739
0