我正在尝试根据名称导入文件,例如:
project /
__init__.py
log.py
conf /
__init__.py
logger_settings.py
other_settings.py
在我的conf/__init__.py
文件中,我想要类似的东西:
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
import os
import sys
import json
def get_settings(identity):
"""Settings."""
try:
from i import *
except ImportError as exc:
raise Exception('Eror importing config %s' % exc)
所以比在log.py
文件中我可以做这样的事情:
#!/usr/bin/env python -u
# -*- coding: utf-8 -*-
# vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab
import os
import logging
from project import conf
CONF = conf.get_settings('logger_settings')
def getLogger(identity ,log_file=CONF.log_file):
# Then access CONF to return settings
# For example:
# host = CONF.host would return something like 'localhost'
我想要logger_settings.py
这样:
log_file = '/mnt/logs/'
host = 'localhost'
我需要如何修改conf/__init__.py
才能实现这一目标?