brianegge
brianegge

Reputation: 29892

Where to store secret keys and password in Python

I have a small Python program, which uses a Google Maps API secret key. I'm getting ready to check-in my code, and I don't want to include the secret key in SVN.

In the canonical PHP app you put secret keys, database passwords, and other app specific config in LocalSettings.php. Is there a similar file/location which Python programmers expect to find and modify?

Upvotes: 3

Views: 3735

Answers (3)

ironfroggy
ironfroggy

Reputation: 8119

Any path can reference the user's home directory in a cross-platform way by expanding the common ~ (tilde) with os.path.expanduser(), like so:

appdir = os.path.join(os.path.expanduser('~'), '.myapp')

Upvotes: 1

S.Lott
S.Lott

Reputation: 391952

A user must configure their own secret key. A configuration file is the perfect place to keep this information.

You several choices for configuration files.

  1. Use ConfigParser to parse a config file.

  2. Use a simple Python module as the configuration file. You can simply execfile to load values from that file.

  3. Invent your own configuration file notation and parse that.

Upvotes: 4

Vinay Sajip
Vinay Sajip

Reputation: 99415

No, there's no standard location - on Windows, it's usually in the directory os.path.join(os.environ['APPDATA'], 'appname') and on Unix it's usually os.path.join(os.environ['HOME'], '.appname').

Upvotes: 3

Related Questions