The Quick Guide To Using Environment Variables in Python | by Yash Prakash | Towards Data Science

In our day to day work, we have all come across the need to hide specific things from our app before sharing them on a public version control system, like Git.
You could be dealing with your SQL database names and URLs, their passwords, some secret keys associated with AWS or Google Cloud Platform user groups, API secrets, the list goes on.
Each app has its own quirks, and there is always a time when we prefer NOT to hardcode some of these application secrets directly into our codebase.
Well, lucky for us, we have the concept of Environment variables, which provide us with a great way to configure our Python application, eliminating the need to make any changes to our source code when dealing with such secrets.
In this little article, I will be sharing the ways through which I prefer using environment variables in my own data science projects.
Here we go 👇
This is one of the easiest ways to configure environment variables for use in Python, or any other language really.
You just export them to be visible in your current bash session — meaning until you’re using the terminal without closing, you’ll be able to access through your code, all the variables you’ve set.
It is as easy as typing and entering:
Please note that it must be entered exactly as I’ve shown, without spaces in between the equals sign on either side and your secret key within double quotes.
Okay, great! This seems simple enough, right?
Next, let me show you one other way to do this but this will be slightly more permanent.
Not gonna lie, it can become pretty inconvenient to set each and every environment variable in your bash one by one, only to lose them when you close the terminal tab.
Also, you need to know how to correctly do it for your operating system as well — apparently, it works differently on Windows than, say, a Unix based system.
Thus, what I normally do is manage all my environment variables through a .env file, which can be stored within my app.
It works exactly the same under each operating system and it’s easy to set up and use with our Python apps too.
Just make a new .env file and set all your variables within and save, like for instance:
And that is it! All your variables can be set once and you can forget about them being associated with the bash altogether.
Also, don’t forget to add this filename in your .gitignore file, in order to protect it from your all-knowing version control system!
Until now, we’ve seen how to set our secret variables in two ways — through our bash session and as a separate file. Now, let’s see how to access them for using in our Python code.
The os.environ.get() function
The os.environ.get() function
If you’re setting up your variables in the bash session, you can access them easily via a code snippet such as:
Here, we get the value of the DATABASE_URL environment variable, also specifying its default value to be : ‘sqlite:////’.
If you leave the default value empty, it will return None instead, if you haven’t set a variable by that name.
If you’ve taken the second route and set your env variables through the .env file, you can instead use a different method to access them in your code.
Install the package simply by this command inside your virtual environment:
pip install python-dotenv
Next, we can access the variables from our .env file through it.
Next, we can easily use the os.environ.get() function to get the variables back through their names, just like in the previous step.
Having set environment variables in your app can spare you a great many headaches (and potentially, heart attacks too :P), as they are useful in hiding the sensitive information inside your .env files, like urls, passwords, and API keys.
I hope this article was helpful in giving you a quick tutorial on using them in your own apps.
Follow me in here to read more articles like these every week! I write frequently about programming in general, and my experiences as a data scientist too. :)
You can also take a peek here for the code and resources related to all of my articles.
A couple other articles of mine that you might want to explore: