Udit Vashisht
Author: Udit Vashisht


Set Passwords and Secret Keys in Environment Variables (Mac/Linux/Windows) - Python Quicktip

  • Aug. 22, 2019, 3:01 p.m.
  • 3 minutes read
  • 148 Views
Set Passwords and Secret Keys in Environment Variables (Mac/Linux/Windows) - Python Quicktip

Hide Passwords and Secret Keys in Environment Variables

If you are into python , there is a fair chance that you would have contributed to open-source or had your code snippets/projects on Github or BitBucket.Some time your code involves some important credentials like passwords or secret keys etc. like the code for our post on how to send emails using python uses google/app password. You surely do not want to hard code the password in your code and accidentaly push it to a remote repository. Hence, the safest way is to do so is saving your secret keys/password in envirnoment variables. In this post we will learn how to save/hide the passwords, secret keys in environment variables for MacOS, Linux and Windows.

The wrong way

Hard coding your username, passwords or secret keys in your code is wrong way and it exposes you to vulnerability. Have a look at the code below:-

# The wrong way 

user_name = 'my_user_name'
password = 'my_password'

print(user_name, password)

# output

my_user_name my_password

Set Passwords and Secret Keys in Environment Variables on Mac/Linux

To set password and secret keys in environment variable on Mac and Linux. You will need to open and modify .bash_profile . To do that open the terminal on your Mac or Linux and cd to the home directory. (You can read about useful terminal commands of mac here)

user desktop $ cd
user ~ $

Now open the .bash_profile using your favorite editor like nano , vim , sublime text , atom etc. You can read a bit more about the text editors here

user ~ $ nano .bash_profile

The following file will open . You may not have the same text like mine there.

set password secret key in environment variable python.png

Now we need to add our environment variables. For that we will have to write the following code. Remember that there is no whitespace on either side of =.

export USER="my_user_name"
export PASSWORD="my_password"

Press ctrl + x and Y to save the nano file.

Now either restart the terminal or use the following command to effect the changes.

user ~ $ source .bash_profile

Now to use these variables in our python script, we will be needing os module. Have a look at the following code. Here instead of hard coding the username and password like the example above, we have used the environment variables and still the result is same.

import os 

user_name = os.environ.get('USER')
password = os.environ.get('password')

print(user_name, password)

# output

my_user_name my_password

Set Passwords and Secret Keys in Environment Variables on Windows

To set the passwords and secret keys in environment variables on Windows, you will have to open Advance System Setting. You can either type ‘Advanced System Setting’ in search bar or browse to it by right clicking My Computer on desktop-> properties -> Advanced System Setting

set password and secret keys in environment variables python.PNG

Now in Advance System Setting you will have to click on Environment Variables and the following screen will appear.

set password and secret keys in environment variables python -.PNG

Now, here we need to add new user variable. So click on new and add both the variables.

set password and secret keys in environment variables python -!.PNG

Now using the same code as above, we can access the environmental variables.

import os 

user_name = os.environ.get('USER')
password = os.environ.get('password')

print(user_name, password)

# output

my_user_name my_password

If you like this tutorial, please feel free to share.



Related Posts

Nested List to list - Python in just three lines of code
By Udit Vashisht | 2 months, 1 week ago

Nested list to list in Python

Nested list in python can be converted to a flat list in python in just three lines of code. Let us assume that you have a following nested list.

[1, 2, [3, 4, [5, 6]], 7, 8, [9, [10]]]

Now, the above list ...

Read More
Python Logging Module - A Primer
By Udit Vashisht | 1 month, 3 weeks ago

Logging in python

Logging is the module in the standard library of python. Logging is an important tool for a programmer or developer, as it gives more insights about the code and any errors in it. In this tutorial, we will learn all about the python logging module. ...

Read More
Python F String - The Ultimate Usage Guide
By Udit Vashisht | 2 months, 3 weeks ago

Python F String

Python’s F-String was added in Python 3.6.0 as PEP-498 – Literal String Interpolation. Before the introduction of Python f-string, there were other methods of formatting like %-formatting, str.format() or string.Template(). These methods have their advantages, but the major disadvantage is that they ...

Read More
Search