我正在尝试通过 init .py文件设置包级别日志记录 我一直在虚拟环境中对此进行测试,但似乎无法理解错误 我的包结构是这样的:
extracts
extracts
__init__.py
logconfig.ini
methods.py
foo.py
setup.py
requirements.txt
setup.py看起来像这样:
setup(name='extracts',
version='0.0.1',
description='Extracts',
packages=['extracts'],
package_data={'extracts': ['*.ini']},
install_requires=requirements,
zip_safe=False
)
init .py导致我出现问题。
logfile = resource_stream(__name__, 'logconfig.ini')
logging.config.fileConfig(logfile)
log = logging.getLogger(__name__)
log.info('Importing extracts 0.0.1')
foo.py是我试图运行的脚本。它从模块' methods.py'导入函数。发生这种情况时,应触发 init .py并设置foo.py的日志记录。不幸的是每次我尝试这个,我都会收到以下错误:
Traceback (most recent call last):
File "test.py", line 2, in <module>
from extracts import methods
File "build/bdist.linux-i686/egg/extracts/__init__.py", line 19, in <module>
logfile = resource_stream(__name__, 'logconfig.ini')
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 942, in resource_stream
self, resource_name
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1346, in get_resource_stream
return BytesIO(self.get_resource_string(manager, resource_name))
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1349, in get_resource_string
return self._get(self._fn(self.module_path, resource_name))
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1425, in _get
return self.loader.get_data(path)
IOError: [Errno 0] Error: 'extracts/logconfig.ini'
https://pythonhosted.org/setuptools/pkg_resources.html#basic-resource-access提到resource_stream(package_or_requirement,resource_name)中的package_or_requirement需要是可导入的。
但我不明白的是,提取物pacakge是可导入的,因为
from extracts import methods
不会失败。任何想法都非常感谢。谢谢。