Hiding secret info in Python using environment variables

Raivat Shah
DataSeries
Published in
4 min readApr 13, 2019

--

A beginner’s key to secrecy…

If you’ve worked on software engineering or data science projects that require some form of API keys, you would’ve noticed a flaw in sharing the code online: the secret information will be out there for everyone to see and possibly misuse! A classic example is a pair of a username and a password for a database. We certainly don’t want that to leak. How do we prevent this?

Environment variables are the answer!

What is an Environment Variable?

Before I answer that, I want you to recall what a variable is in a programming language. Simply put, it is a quantity in a program that can change over time.

An environment variable is similar to that definition, albeit with a few nuances. The “environment” in “environment variable” refers to the environment your program is running in (either your computer (local) or server (remote)). As you can guess, an environment variable has something to do with the environment the program is running in. Thus, unlike a variable of a programming language, the environment variable is tied to your environment (of your operating system) and not any particular programming language. Thus, you can reuse them in different languages.

--

--

Raivat Shah
Raivat Shah

Written by Raivat Shah

Digital Nomad. Curious entrepreneurship, tech, finance, travel, cricket and more ...