How to use environment variables in Python natively?

Python using .env variables.

Why use a .env when working on a project?

You’re trying to work on your side project in python, and you have come to the point where you are trying to refactor your code, and you realize it’s not a great idea to have all your configuration variables out there in the open. According to the manifesto, you store them an env variable as they can scale up as the app expands over time, and they are straightforward to change between deployments without changing the code.

Using python natively.

As a beginner, I try to limit my use of libraries and figure out native python ways to solve any issues. Let me introduce another great feature of python using a config parser.

Config parser => Config Files.

This module lets you manage config files for your application.

“This module provides the ConfigParser class, which implements a basic configuration language that provides a structure similar to what’s found in Microsoft Windows INI files. You can use this to write Python programs which can be customized by end-users easily.”

Instructions:

Step 1. Create the .env file.

The structure of the .env consists of sections that must be led with a [section] header as like this:

And must be followed by either an = or :

[‘local’] = section header

[‘user’] = key.

That’s all folks!

I’m always looking to meet more engineers in my field, find me on Linkedin and let’s connect!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store