How do I install a user-specific configuration file with the distribute python

312 views Asked by At

I'm creating a python package, and using distribute to package it as an egg. I'd like to install a user-editable configuration file for each user of my software. What is the best way to accomplish this? I'm using Python 2.6 and am targeting the Windows platform.

2

There are 2 answers

0
S.Lott On BEST ANSWER

Since the egg is hard to edit, it doesn't go in the egg.

A user-editable configuration file goes in the user's HOME directory or a system-wide directory like /etc/myapp.

Your app should search in a number of easy-to-find places. Follow the Linux guidelines for tracking down the .bashrc file for hints on how best to do this. Or Follow the Windows guidelines on System and My Documents directoryes.

You write your app in one of two ways.

  1. It can work with no config. If -- after searching all the usual places -- there's no config, it still works.

  2. It creates an default config in the current working directory if it can't find one anywhere else.

1
Apalala On
os.path.expanduser(path)

On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user‘s home directory.

http://docs.python.org/library/os.path.html

The *nix idiom of using configuration file names starting with '.' is now being used on Windows as well, as the latest versions of Windows treat them as hidden files.

import os
config_filename = os.path.expanduser('~/.myappconf)