Best way to store config or preferences in a multi-platform way.

Brian Vanderburg II BrianVanderburg2 at aim.com
Thu May 1 08:18:12 EDT 2008


Lance Gamet wrote:
> Hi, python beginner starting a new project here.
>
> This project will store most of its actual data in a shared-database, but 
> I have a small amount of user specific data that I need to be stored like 
> configuration or preferences for example, the list of databases that the 
> program should connect to.
>
> On Unix this might be a .file, on windows this could be in the registry, 
> or an ini file or an xml file in ProgramData or AppData or something.
>
> Is there a pythony way to store such config data, perhaps there is 
> already a standard python package for such a purpose?
>
> My app uses Qt, and Qt has its method of doing it (QSettings), but for 
> architectural reasons I don't want to use it.
>
> Could sqlite be an option perhaps? I am still undecided if the ability 
> for the user to edit the file independently of the program is a good or 
> bad thing.
>
> Thanks a lot.
> Lance
> --
> http://mail.python.org/mailman/listinfo/python-list
>   
Lance Gamet wrote:
> Hi, python beginner starting a new project here.
>
> This project will store most of its actual data in a shared-database, but 
> I have a small amount of user specific data that I need to be stored like 
> configuration or preferences for example, the list of databases that the 
> program should connect to.
>
> On Unix this might be a .file, on windows this could be in the registry, 
> or an ini file or an xml file in ProgramData or AppData or something.
>
> Is there a pythony way to store such config data, perhaps there is 
> already a standard python package for such a purpose?
>
> My app uses Qt, and Qt has its method of doing it (QSettings), but for 
> architectural reasons I don't want to use it.
>
> Could sqlite be an option perhaps? I am still undecided if the ability 
> for the user to edit the file independently of the program is a good or 
> bad thing.
>
> Thanks a lot.
> Lance
> --
> http://mail.python.org/mailman/listinfo/python-list
>   
One way I've just started to use it to create a utility function to 
return the location of the user data folder, depending on the operating 
system:

import os, sys

def GetUserDataDirectory():
    dir = None

    # WINDOWS
    if os.name == "nt":

       # Try env APPDATA or USERPROFILE or HOMEDRIVE/HOMEPATH
       if "APPDATA" in os.environ:
          dir = os.environ["APPDATA"]

       if ((dir is None) or (not os.path.isdir(dir))) and ("USERPROFILE" 
in os.environ):
          dir = os.environ["USERPROFILE"]
          if os.path.isdir(os.path.join(dir, "Application Data")):
             dir = os.path.join(dir, "Application Data"))

       if ((dir is None) or (not os.path.isdir(dir))) and ("HOMEDRIVE" 
in os.environ) and ("HOMEPATH" in os.environ):
          dir = os.environ["HOMEDRIVE"] + os.environ["HOMEPATH"]
          if os.path.isdir(os.path.join(dir, "Application Data")):
             dir = os.path.join(dir, "Application Data"))

       if (dir is None) or (not os.path.isdir(dir))
          dir = os.path.expanduser("~")

       # One windows, add vendor and app name
       dir = os.path.join(dir, "vendor", "app")

    # Mac
    elif os.name == "mac": # ?? may not be entirely correct
       dir = os.path.expanduser("~")
       dir = os.path.join(dir, "Library", "Application Support")
       dir = os.path.join(dir, "vendor", "app")

    # Unix/Linux/all others
    else:
       dir = os.path.expanduser("~")
       dir = os.path.join(dir, ".app")
       # Some applications include vendor
       # dir = os.path.join(dir, ".vendor", "app")

    return dir

   
Brian Vanderburg II
   



More information about the Python-list mailing list