Saturday, October 15, 2016

Handling Passwords in Python Code

One problem with dealing with databases is that we need a user name and password. We don't want to check those into version control, or code them into the program, so how to handle them?

One simple method is to have the user set environment variables with their user name and password, and have the python code read those vars and use their values. That way, your user name and password don't have to be checked into the repo.

So I changed my database connect code to this:

    # Open database connection
    db = MySQLdb.connect("localhost", os.environ['DB_USER'], os.environ['DB_PASSWORD'],
                         "tempdb" )

Now I need to define environment vars DB_USER and DB_PASSWORD before running the code.

No comments:

Post a Comment