python single configuration file

前端 未结 4 683
旧巷少年郎
旧巷少年郎 2021-01-31 18:50

I am developing a project that requires a single configuration file whose data is used by multiple modules.
My question is: what is the common approach to that? should i rea

4条回答
  •  忘了有多久
    2021-01-31 19:13

    If you want to share your config across different machines, you could perhaps put it on a web server and do import like this:

    import urllib2
    confstr = urllib2.urlopen("http://yourhost/config.py").read()
    exec(confstr)
    

    And if you want to share it across different languages, perhaps you can use JSON to encode and parse the configuration:

    import urllib2
    import simplejson
    confstr = urllib2.urlopen("http://yourhost/config.py").read()
    config = simplejson.loads(confstr)
    

提交回复
热议问题