I have a python application that requires database credentials, the code looks something like this.
def __init__ (self): self.conn = pymysql.connect("localhost","user","pass","db", use_unicode=True, charset="utf8")
As you can see, I am hard-coding the sensitive data, and when I push this code to github I have to remove it, and when I run it in the server, I have to modify it. So, basically I have to edit this file for prod/dev environment.
I know we can store variables in Linux as
to retrieve the value, but how do I use this in my python application?
You can use envparse module . It allows you to use environment variables and cast them to the proper types.
You can add variables for each value, as database name, database host, or create a postprocessor and define the variable as an URL:
from envparse import env db_host = env(DB_HOST, 'localhost') db_port = env.int(DB_PORT, 3306) db_user = env(DB_USER) db_pass = env(DB_PASS) db_name = env(DB_NAME) conn = pymysql.connect(db_host,db_user,db_pass,db_name, use_unicode=True, charset="utf8")