Ruan Bekker's Blog

From a Curious mind to Posts on Github

Reference Credentials Outside Your Main Application in Python

In this post I will show one way of referencing credentials from your application in Python, without setting them in your applications code. We will create a seperate python file which will hold our credentials, and then call them from our main application.

Our Main Application

This app will print our username, just for the sake of this example:

app.py
1
2
3
4
5
6
from config import credentials as secrets

my_username = secrets['APP1']['username']
my_password = secrets['APP1']['password']

print("Hello, your username is: {username}".format(username=my_username))

Our Credentials File

Then we have our file which will hold our credentials:

config.py
1
2
3
4
5
6
credentials = {
        'APP1': {
            'username': 'foo',
            'password': 'bar'
            }
        }

That is at least one way of doing it, you could also use environment variables using the os module, which is described here

References: