How to do module configuration properly

Jan Kosinski jan.kosinski at gmail.com
Fri Oct 22 07:22:38 EDT 2010


I have created a python module, which contains a bunch of utility functions that use a number of global variables (directory and file names, etc.).

I want to move that global variables to an external configuration file and I want to load all global variables from that configuration file when module is imported.

I am not sure which is the proper way of doing that. At the moment I use the solution as in the following example, is it fine?

#my_module.py:
import ConfigParser

config = ConfigParser.SafeConfigParser()
config_f = open('my_module.cfg')
config.readfp(config_f)
config_f.close()
global_var = config.get('section', 'global_var')

def some_func():
    print global_var

#my_script.py:
import my_module

my_module.some_func()

#my_module.cfg:
[section]
global_var = /tmp/




More information about the Python-list mailing list