[Tutor] where to put configuration files

Jeff Shannon jeff@ccvcorp.com
Mon Jun 9 13:54:02 2003


Paul Tremblay wrote:

>This configuration file is kind of a pain to have to maintain. Is it
>okay to just dump configuration files in /etc, or is this considered bad
>practice?
>  
>

The problem is that there is no good cross-platform way to store 
configuration files.  Unix (and unix-like OSes such as Linux and 
presumably Mac OS X) can store config information in /etc, or in the 
application directory, or even in the user's home directory.  Windows 
can have INI files in the application directory, in C:\Windows, or maybe 
even in C:\Windows\System (or System32), but are better off storing such 
configuration information within the registry.  

The wxWindows/wxPython cross-platform GUI library has a wxConfig class 
which transparently handles storage in the appropriate place.  The 
Python standard library has a ConfigParser module to assist in managing 
configuration files, but it looks to have a lot less cross-platform 
transparency than wxConfig (and doesn't look like it handles registry 
information at all).  Still, you may want to investigate ConfigParser, 
it might simplify your maintenance tasks.  (wxConfig will only simplify 
things if you're already using the wxPython library.)  

In general, I don't think you can do this safely cross-platform without 
doing a check of what platform you're running on, and using a different 
configuration back-end based on that.  However, if you're fairly 
confident that your script will only be running on Unix, you're probably 
fairly safe trying to write your config file somewhere into /etc 
(probably in an application-specific subdirectory, i.e. 
/etc/myapp/myapp.cfg).  Depending on the nature of the script, it might 
also be a good idea to allow it to fall back on a config file in the 
application's install directory (which might allow it to be installed in 
the home directory of someone without root privelidges).  One way to 
determine the install directory is to look at where your modules are 
being imported from -- IIRC, modules have a __file__ attribute that will 
give the path and filename that the module was loaded from.  It should 
be fairly easy to extract the path and use that as a location for a 
config file.

It does seem to me that a transparent cross-platform config manager, a 
la wxConfig, would be a very useful addition to the Python standard library.

Jeff Shannon
Technician/Programmer
Credit International