Where to put a configuration file in Python?

前端 未结 7 1728
孤独总比滥情好
孤独总比滥情好 2020-12-22 17:52

In development mode, I have the following directory tree :

| my_project/
| setup.py
| my_project/
    | __init__.py
    | main.py
    | conf/
        | mypro         


        
相关标签:
7条回答
  • 2020-12-22 18:12

    If you're using setuptools, see the chapter on using non-package data files. Don't try to look for the files yourself.

    0 讨论(0)
  • 2020-12-22 18:16

    The docs for Writing the Setup Script, under section 2.7. Installing Additional Files mention:

    The data_files option can be used to specify additional files needed by the module distribution: configuration files, message catalogs, data files, anything which doesn’t fit in the previous categories.

    0 讨论(0)
  • 2020-12-22 18:17

    I don't think there is a clean way to deal with that. You could simply choose to test for the existence of the 'local' file, which would work in dev mode. Otherwise, fall back to the production path:

    import os.path
    
    main_base = os.path.dirname(__file__)
    config_file = os.path.join(main_base, "conf", "myproject.conf")
    
    if not os.path.exists(config_file):
        config_file = PROD_CONFIG_FILE   # this could also be different based on the OS
    
    0 讨论(0)
  • 2020-12-22 18:18

    Another option is to keep all the .cfg and .ini files in the home directory, like 'boto' does.

    import os.path
    config_file = os.path.join(os.path.expanduser("~"), '.myproject')
    
    0 讨论(0)
  • 2020-12-22 18:19

    Have you seen how configuration files work? Read up on "rc" files, as they're sometimes called. "bashrc", "vimrc", etc.

    There's usually a multi-step search for the configuration file.

    1. Local directory. ./myproject.conf.

    2. User's home directory (~user/myproject.conf)

    3. A standard system-wide directory (/etc/myproject/myproject.conf)

    4. A place named by an environment variable (MYPROJECT_CONF)

    The Python installation would be the last place to look.

    config= None
    for loc in os.curdir, os.path.expanduser("~"), "/etc/myproject", os.environ.get("MYPROJECT_CONF"):
        try: 
            with open(os.path.join(loc,"myproject.conf")) as source:
                config.readfp( source )
        except IOError:
            pass
    
    0 讨论(0)
  • 2020-12-22 18:20

    Update for S.Lott's answer.

    Now the method configparser.ConfigParser.readfp() is deprecated。

    You can use configparser.ConfigParser.read() method directly for multi files.

    For example:

    import configparser
    
    config = config = configparser.ConfigParser()
    
    all_config_files = ['/path/to/file1', '/path/to/file2', '/path/to/file3']
    
    config.read(all_config_files)
    

    Note:

    • The config options in later config file will overwrite the previous.
    • If the config file not exists, read() will ignore it.
    • If you have required options need read from a file, use read_file() first.

    configparser.ConfigParser.read()

    0 讨论(0)
提交回复
热议问题