Adding and reading a config.ini file inside python package -


i writing first python package want upload on pypi. structured code based on blog post.

i want store user setting in config.ini file. read once(every time package run) in separate python module in same package , save user setting in global variables of module. later import in other modules.

to recreate error edited few lines of code, in template described in blog post. (please refer since take typing recreate entire thing here in question.)

the difference stuff.py reads config file this:

from configparser import safeconfigparser  config = safeconfigparser() config.read('config.ini')  test_key = config.get('main', 'test_key') 

here contents of config.ini (placed in same dir stuff.py):

[main] test_key=value 

and bootstrap.py imports , print test_key

from .stuff import test_key  def main():     print(test_key) 

but on executing package, import fails give error

traceback (most recent call last):     file "d:\coding\bootstrap\bootstrap-runner.py", line 8, in <module>     bootstrap.bootstrap import main     file "d:\coding\bootstrap\bootstrap\bootstrap.py", line 11, in <module>     .stuff import test_key     file "d:\coding\bootstrap\bootstrap\stuff.py", line 14, in <module>     test_key = config.get('main', 'test_key')     file "c:\python27\lib\configparser.py", line 607, in     raise nosectionerror(section) configparser.nosectionerror: no section: 'main' 

import keeps giving configparser.nosectionerror, if build/run stuff.py(i use sublime3), module gives no errors , printing test_key gives value output.

also, method of import work when use 3 files(config, stuff, main) in dir , execute main script. there had import this

from stuff import test_key 

i'm using explicit relative imports described in post don't have enough understanding of those. guess error due project structure , import, since running stuff.py standalone script raises no configparser.nosectionerror.

other method read config file once , use data in other modules helpful well.

abhimanyupathania : issue path of config.ini in stuff.py. change config.read('config.ini') config.read('./bootstrap/config.ini') in stuff.py. tried solution. works me.

enjoying pythoning...


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -