Environment Variables in Python

Environment Variables in Python

Want to improve security in your code? Want to use workflow’s best practice? You better get familiar with environment variables!

When working on a project of any scale, we often encounter configurations that have to be “hard-coded” to run - passwords, usernames, server \ DB credentials, authentication keys, and more. But altho they are a must, we don’t want them to be exposed publically or leaked. And with those security limitations, we still must be able to run the project in different environments and conditions.

This is why Environment Variables, also known as env var, are used. In this article, we will show how to use them properly. Hopefully, at the end of it, one more important tool will be added to your coding toolkit!

Environment Variables - as the name implied, are variables set on the environment currently used - local, virtual or remote. The main feature is that in the chosen environment the value of a certain variable is set and could be accessed from anywhere in the code, yet is not related to the code itself.

For example - we can set the value of the variable “db_password” to be “xyz1234” (if this is your password, change it now…) to keep the password to our DB discreet inside the current environment. Every time? “db_password” will be called, we will get the password to connect to the DB, but at the same time, we don’t expose it in the code and avoid any mishaps.

“But when should I use them…?”

Glad you asked! The first thing to take in mind is that the env vars, as mentioned before, are not part of the project’s code so when dealing with variables that change based on the environment, the usage of environment variables is recommended.

How to summon your Environment Variables!

In python, os.environ will be used - a dictionary containing all the environment variables. To retrieve a certain var, “db_password”? for reference, we will say “ db_password I choose you!” and the password will be summoned right into our code!

Just kidding, use python’s standard dictionary syntax, easy is that.

First, the well-known “os” library has to be imported.

There are several methods to retrieve env vars:

#1

Using brackets - Call os.environ with [ ] and the name of the variable inside the brackets.

No alt text provided for this image

One drawback to this method of retrieving, if the var is not set it will raise an error.

#2

Using get - Call os.environ with the get function, to avoid the error can set the default value it will return if the var is not found.

No alt text provided for this image

The default value if not set (in the example it’s “1212”) is None.

Set your environment variables

Note: the value must be a string so if you keep any other type, make sure to use casting when saving \ when calling the env var.

#1

With this method of setting env var, we will be using scripts.

Ok so this is a complicated one, just keep calm and go step by step:

No alt text provided for this image

Yea, it is that simple!

All you need is to call os.environ with brackets containing the name of the variable you want to set, and just like in a dictionary it will be set.

Now, this script could be gitignored, or just in a different folder and could run only once to set the values and that’s it, they are good to go from now on.

#2

Here we will show how to use env var in a virtual env (PyCharm on windows in this case)

In the “vnv” folder you will find “Scripts”, this folder contains the activation and deactivation process of the virtual env.

Located the file “activate.bat” and insert the env var with the set command.

Like so:

No alt text provided for this image

And that’s it!

Now when running the virtual env this var, test, is set and when calling it, will return “1212”.

Note: After updating the “activate.bat” we will have to deactivate the virtual env using the “venv\Scripts\deactivate.bat” command (on windows) and then activate the virtual env again “venv\Scripts\activate.bat”


Netanel Stern

CEO and security engineer

6 个月

???? ??? ?? ?? ?????? ??????? ??? ???? ???? ????? ???? ?????? ???: https://chat.whatsapp.com/HWWA9nLQYhW9DH97x227hJ

回复
Itamar Kraitman

Account Manager at GurMob | User Acquisition & Monetization Expert | In-App Advertising | Mobile Performance Marketing

2 年

?????!! ?????? ????. ???? ???!

回复
Anna Sandler-Maatok

Senior Security Engineer @ Morgan Stanley || Ms Cybersecurity Risk management @ Georgetown University

2 年

????!

回复
Anna Chaykovsky

Software Engineer at PANW ?? | Backend Storyteller ?? | "Becoming Developers" (Baot) Community Manager ????♀? | B.Sc Computer Science?? | Moshal Alumna??????| Microservices ?? | Docker ?? | Cloud ??

2 年

?? ?????, ????! ???? ????.

回复

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

Omer Rugi的更多文章

  • Gender Classification From Text Using ML

    Gender Classification From Text Using ML

    The drive behind the project As part of my ongoing learning process in the field of ML\DL, I took this task to play…

    28 条评论

社区洞察

其他会员也浏览了