- Published on
Reference Credentials Outside Your Main Application in Python
- Authors
- Name
- Ruan Bekker
- @ruanbekker
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
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
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:
- https://docs.python.org/2/tutorial/inputoutput.html
- https://docs.python.org/2/library/os.html#os.environ
Thank You
Thanks for reading, feel free to check out my website, feel free to subscribe to my newsletter or follow me at @ruanbekker on Twitter.
- Linktree: https://go.ruan.dev/links
- Patreon: https://go.ruan.dev/patreon