Where to put a configuration file in Python? Where to put a configuration file in Python? python python

Where to put a configuration file in Python?


Have you seen how configuration files work? Read up on "rc" files, as they're sometimes called. "bashrc", "vimrc", etc.

There's usually a multi-step search for the configuration file.

  1. Local directory. ./myproject.conf.

  2. User's home directory (~user/myproject.conf)

  3. A standard system-wide directory (/etc/myproject/myproject.conf)

  4. A place named by an environment variable (MYPROJECT_CONF)

The Python installation would be the last place to look.

config= Nonefor loc in os.curdir, os.path.expanduser("~"), "/etc/myproject", os.environ.get("MYPROJECT_CONF"):    try:         with open(os.path.join(loc,"myproject.conf")) as source:            config.readfp( source )    except IOError:        pass


The appdirs package does a nice job on finding the standard place for installed apps on various platforms. I wonder if extending it to discover or allow some sort of "uninstalled" status for developers would make sense.


If you're using setuptools, see the chapter on using non-package data files. Don't try to look for the files yourself.