Where to put a configuration file in Python?

前端 未结 7 1765
孤独总比滥情好
孤独总比滥情好 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: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
    

提交回复
热议问题