Skip to content

Instantly share code, notes, and snippets.

@chrislawlor
Created October 23, 2013 10:57
Show Gist options
  • Save chrislawlor/7116554 to your computer and use it in GitHub Desktop.
Save chrislawlor/7116554 to your computer and use it in GitHub Desktop.
Get database password from ~/.my.cnf file, if it exists. Useful when developing on CloudSQL + AppEngine, which gleefully ignores environment variables.
# Get password from ~/.my.cnf if it exists
my_cnf_path = os.path.expanduser('~/.my.cnf')
if os.path.exists(my_cnf_path):
from ConfigParser import SafeConfigParser
config = SafeConfigParser()
with open(my_cnf_path) as my_cnf:
config.readfp(my_cnf)
try:
DATABASES['default']['PASSWORD'] = config.get('client', 'password')
except ConfigParser.NoOptionError:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment