I normally use ConfigParser and store passwords as hashes in a text file. I then retrieve the hash from the text file and decode it inside my scripts, then send it to whatever needs it as a property of a Config object.
Here's an example of how the config parser class would look like:
import configparser
import os
cwd = os.path.dirname(__file__)
class UserEntity:
def __init__(self, user=None,
password=None):
config = configparser.ConfigParser()
config_file = 'users.cfg'
config_location = os.path.join(cwd, config_file)
with open(config_location, 'r') as cfg:
config.read_file(cfg)
if not user:
self.user = config['credentials']['user']
else:
self.user = user
if not password:
self.password = config['credentials']['password']
else:
self.password = password
usr = UserEntity()
print(usr.password, usr.user)
usr2 = UserEntity(user='Test')
print(usr2.password, usr2.user)
usr3 = UserEntity(user='Test', password='eb65af66881341099ebf28dad3800d0f')
print(usr3.password, usr3.user)
Do note that you will need "users.cfg" to be present in the same folder as the script running this code for it to work
The users.cfg file looks like this
[credentials]
user=user123
password=password123
To parse and store the passwords encrypted, you could use implementations listed here:
Simple way to encode a string according to a password?
You can use the resulting code inside a function that generates the hash and then writes it to a config file. Your other scripts can then use that file as a config like in the first example. Use the decrypt functionality to use the password wherever you need without ever showing it in plain text anywhere.