Escualo
Escualo

Reputation: 42082

Defining path to module's configuration files

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?

Upvotes: 2

Views: 254

Answers (1)

awesomo
awesomo

Reputation: 8816

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').

Upvotes: 2

Related Questions