My python package directory structure shown as below:
./my_package
|---./lib
| |----__init__.py
| |----tools.py
|
|----__init__.py
|----my_package.py
|----setting.conf
In the tools.py :
import ConfigParser
config = ConfigParser.RawConfigParser()
config.read('setting.conf')
debug = config.getboolean('default', 'debug')
I don't know why config.read is work. I can get debug value in section default.
Your test works because the configuration file is in the current directory, because you're running your main module from the directory of the configuration file.
But if you're using another main file located somewhere else, that could fail.
Here's a clean way to make it work in any case: since your tool is one level below the configuration file, the path to the configuration file can be computed as follows from the tools.py
module:
conf_file = os.path.join(os.path.dirname(os.path.dirname(__file__)),'setting.conf')
os.path.dirname(__file__)
yields the directory where tools.py
is located. Perform one more dirname
to get the directory where the conf file is located. Now compute absolute name of config file using os.path.join