/plushcap/analysis/vonage/vonage-python-environment-variables-a-primer

Python Environment Variables (Env Vars): A Primer

What's this blog post about?

You can store environment variables in your operating system, but there are other ways to use them that we will learn about shortly. Environment variables allow you to change a program's behavior without changing the program itself. To follow along with this tutorial, you'll need to have Python installed on your machine. You get and set environment variables in Python using the built-in os module. You can view all of the environment variables in your program by saving the following code in a Python file and then running this Python program: import os; os.environ. Your Python interpreter should print out all your operating system's environment variables when running this code. You can access the different environment variables in os.environ like a Python dictionary. To set environment variables in Python, you can add them to the os.environ object like you would with a dictionary. However, only strings are permitted, as these are passed directly to the shell your interpreter is running in. There are different reasons that you might want to store environment variables, so there are also different ways to store them. You can store environment variables on your operating system, in files, in the cloud, in CI/CD systems, or with a universal secrets manager. Store environment variables on your operating system using the command line. This creates an environment variable and sets its value. Store environment variables in files by creating a .env file to persist them. You can use a tool like dotenv to load environment variables from a .env file into os.environ. When you create software for production, you probably won't run it from your computer, so you need to know how to set and get environment variables from wherever you run your code in production. You can store environment variables in CI/CD systems like GitHub Actions or CircleCI, which allows you to simplify testing and debugging by adjusting environment variables rather than changing the code. Store environment variables with a universal secrets manager like Doppler, which allows you to store your secrets in one place so everyone on your team can access them. Finally, when using external APIs, you might need to use environment variables to get the tooling set up optimally.

Company
Vonage

Date published
Nov. 7, 2023

Author(s)
Max Kahan

Word count
1920

Language
English

Hacker News points
None found.


By Matt Makai. 2021-2024.