Environment variables

Environment variables

Environment variables are variables that store data in your program but outside your code. For example, key and secret for an API.

I want this information to be stored outside my source code, so I can keep sensitive data safe. I don't want to expose my credentials inside my open-source code. In addition, I don’t want to touch my code each time API credentials have changed.

I can access already set environment variables like “USER” for example, using os.environ["USER"] or os.environ.get("USER"), I can add my own environment variable like: os.environ['API_USER'] = 'username'

Cool for now

When I create a new environment variable, it only exists for that session. When I close the terminal/program, the environment variable no longer exists. How can I make these variables persist?

Persistence

My preferred way is to use python-dotenv library

First install the library

pip3 install python-dotenv        

then create a .env file in the same directory as the Python file resides.

Next, create a variable inside .env file like this: Name = "value"

Back into the code, I will import the library

from dotenv import load_dotenv        

calling load_dotenv function, brings environment variables into os.environ, and now I can access them.

print(os.environ["API_USER"])        

Cool for now

What if I have two environments, each has its own keys. How can I verify Production environment is using Production keys and Development environment is using Development keys?

I can specify more than one .env file, and direct load_dotenv to locate the correct .env file.

For example:

home = Path().absolute(


dev_path = Path(f'{home}/DEV/.env')


prod_path = Path(f'{home}/PROD/.env'))        

when I want to load the Development environment keys, I will use

load_dotenv(dotenv_path=dev_path)        

and when I want to load the Production environment keys, I will use

load_dotenv(dotenv_path=prod_path)        

By default, load_dotenv doesn't override existing environment variables, so in case I have two different environments, either I can use different keys

No alt text provided for this image

Or in case I want to use the same key in both .env files, I will need to call load_dotenv with override=True to allow overriding the environment value

No alt text provided for this image
Mohamad Abo-Ras

Software Engineer at F5

2 年

Very simple and useful article, thanks for sharing :)

要查看或添加评论,请登录

Orsan Awawdi的更多文章

  • Docker Disk Space Management

    Docker Disk Space Management

    It is necessary to regularly check the disk space occupied by Docker to ensure efficient resource management and…

  • Code Review

    Code Review

    Code Review is a sensitive matter. It introduces the code you wrote to the eyes of another person, who has their own…

  • re.findall

    re.findall

    When we talk about finding recurrent text in a string, we think about regex (Regular Expressions). Regex has so many…

    1 条评论
  • AI generated code

    AI generated code

    Should we always listen to AI generated code? I asked BLACKBOX.AI to write me a simple code in #python.

  • Understanding type annotation in Python

    Understanding type annotation in Python

    Why do we need type hints in Python? We can annotate (comment) variables and functions with data types. Being a…

  • Nested Repeaters

    Nested Repeaters

    Let's take an example. We have Categories table in our DB, and each Category has multiple subcategories.

  • Process transcript with Python

    Process transcript with Python

    Let's see what is the most popular word from Donald Trump speech. Transcript of the speech can be found when googling…

  • Three options to filter a list in C#

    Three options to filter a list in C#

    I will show here three different ways to filter text by some string criteria in C#. We have a public class called…

  • Validation using Attributes in C#

    Validation using Attributes in C#

    Validating data entered by user can be done via multiple methods. Attributes is one powerful yet simple way to validate…

社区洞察

其他会员也浏览了