Store Password Credentials With Python And GPG

September - 2020

NOTE: This is no longer the way I do stuff. I use the system keychains see this post for details


To get credentials from your encrypted store, use this:

This is the original that only works in Python 3

def get_credential(file_path): import subprocess return str(subprocess.run([ '/usr/local/bin/gpg', '--armor', '--decrypt', '-q', file_path ], stdout=subprocess.PIPE).stdout.decode('utf-8')).split("\n")[0]

This should work with both 2 and 3

def get_credentials(file_path): import subprocess return subprocess.check_output([ '/usr/local/bin/gpg', '--decrypt', '-q', file_path ]).decode('utf-8').split("\n")[0]

credential = get_credentials('/Users/alans/credentials/example_credential.asc')