views:

25

answers:

1

A Python module I'm developing has a master configuration file in /path/to/module/conf.conf. The /path/to/module/ depends on the platform (for instance, /Users/me/module in OS X, /home/me/module in Linux, etc).

Currently I define the /path/to/module in __init__.py, where I use logic:

if sys.platform == 'darwin':
 ROOT = '/Users/me/module'
elif sys.platform == 'linux':
 ROOT = '/home/me/module'
# et cetera

Once I have the configuration file root directory, I can open conf.conf anywhere I want.

Do you have a better methodology?

+1  A: 

Inside of your __init__.py, you could get the directory where the __init_.py script lives using the __file__ magic variable like so:

from os.path import dirname
ROOT = dirname(__file__)

Then you know that conf.conf will be located at os.path.join(ROOT, 'conf.conf').

awesomo
Excelent - exactly what I needed. Thanks.
Arrieta