views:

294

answers:

3

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?

+2  A: 

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').

Vinay Sajip
+3  A: 

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.

S.Lott
+1 for the answer, but why would you use execfile over a standard import?
ChristopheD
Because I want the "globals" loaded into a dictionary I created. It's not a proper module and I generally don't want anything but a few globals.
S.Lott
+1  A: 

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')
ironfroggy
Not quite. On my Windows machine, `os.path.expanduser('~')` expands to `C:\Documents and Settings\Vinay Sajip` which is not the recommended place to put application data. That recommended place is specified by the environment variable `APPDATA`, as I mentioned in my answer.Note that the question was asking where to store application configuration data, not how to find the user's home directory. Not the same thing!
Vinay Sajip