When working with APIs, an ‘API token’ or ‘key’ is often required to use the API. If you are working with these files locally this is not an issue, as long as your machine is secure. However, if you are using a version control system like
git (which you should be doing) and you push to a public repository, its no longer a good idea to store API keys in your source code files as they can be seen by anybody.
I came across this issue when working on urban-enigma, which is machine learning project which trys to predict the location where a picture was taken. It is trained on Instagram pictures that are geotagged. I realised that I did not want to store my secret API keys on a public repository, so enivronment variables came to the rescue.
According to Wikipedia:
An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer.
You can think of it as a variable that can be accessed by any program that is in that environment. So how do you set these environment variables?
Using environment variables in Windows is easy on the command line.
So to set the variables use
setx and if you want to recall the variable, remember to wrap it in percentage signs, like
Linux and OS X
Setting environment variables in Linux and OS X is also done on the command line.
Setting variables is done with
export VARNAME=VAR_VALUE and they can be recalled on the prompt with just
$VARNMAME, no need for the percentages when using bash.
Using environment variables in Python
Interfacing with enviroment variables is done with the
os library. Using the variables that we set above, the syntax is as follows:
You can now see how to use this for storing API keys and the like.
Setting environment variables with a bash script
Environment variables are only persistent for that shell session, so when you close the terminal window they are lost. It can get tedious setting this variables everytime you make a new terminal, so you can make a script to do this. An example script is below
Then save the script, I will save it as
.setenv.sh. I added the dot before just to make it a bit harder to find. First make the script executable with
chmod +x .setenv.sh and then run the script with
. .setenv.sh. The
. scriptname may seem like intersting syntax but you must do this to make the script run in the current shell so the variables are saved. If you run the script with
./scriptname it will run the script in a new shell so the variables won’t be saved. Alternative syntax for this is to use
source scriptname and the
. scriptname is actually an alias for
Remember to not add this script to your repository, or there is no point in using environment variables!
So I hope after reading this you now know how and when to use environment variables with Python.