Hiding API keys in python using anaconda environment variables

First you should have anaconda 😉
Then we need to find out where the environment is located
We’ll create create the sub-directories
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh

We’ll have to input the variables in
For example here we will add a secret key and a path
export MY_KEY='secret-key-value'
export MY_FILE=/path/to/my/file/

Also important to unset the variables when we deactivate the environment we will need to edit the file
and we will add the following commands

unset MY_KEY
unset MY_FILE

If we are inside the environment and we run python we can import the key and the path the following way
import os

and you will get -for this example- as output
This is the way to prevent committing to the repo private keys or private information

Newsletter subscription

Leave a Reply

Your email address will not be published. Required fields are marked *