在Python Egg中访问配置文件时出现问题 [英] Problem accessing config files within a Python egg

查看:167
本文介绍了在Python Egg中访问配置文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下结构的Python项目:

package1
  class.py
  class2.py
  ...
package2
  otherClass.py
  otherClass2.py
  ...
config
  dev_settings.ini
  prod_settings.ini

我写了一个setup.py文件,将其转换为具有相同文件结构的egg. (当我使用zip程序检查它时,结构看起来是相同的.)有趣的是,当我从IDE中运行Python代码时,它可以正常工作并且可以访问配置文件.但是当我尝试使用egg从其他Python脚本运行它时,似乎无法在egg中找到配置文件.如果我将配置文件放到相对于 calling Python脚本(鸡蛋外部)的目录中,它可以工作-但这种做法违背了拥有一个具有所有功能的自包含鸡蛋的目的.该程序的功能,可以从任何地方调用.只要它们不使用配置文件,我就可以使用任何类/模块并从egg运行任何功能...但是,如果它们使用了,egg就找不到它们,因此这些功能将不起作用. /p>

任何帮助将不胜感激!我们对这里的鸡蛋有点陌生,真的不知道从哪里开始.

解决方案

问题是,配置文件不再是文件,而是打包在egg中.在文档中找到答案并不容易,但是就在那里.从 setuptools开发人员指南:

通常,现有程序会处理程序包的__file__属性,以便找到数据文件的位置.但是,此操作与基于PEP 302的导入挂钩不兼容,包括从zip文件和Python Eggs导入.

要访问它们,您需要遵循说明资源管理API.

在我自己的代码中,日志记录配置文件存在此问题.我这样成功使用了API:

from pkg_resources import resource_stream

_log_config_file = 'logging.conf'
_log_config_location = resource_stream(__name__, _log_config_file)
logging.config.fileConfig(_log_config_location)
_log = logging.getLogger('package.module')

I have a Python project that has the following structure:

package1
  class.py
  class2.py
  ...
package2
  otherClass.py
  otherClass2.py
  ...
config
  dev_settings.ini
  prod_settings.ini

I wrote a setup.py file that converts this into an egg with the same file structure. (When I examine it using a zip program the structure seems identical.) The funny thing is, when I run the Python code from my IDE it works fine and can access the config files; but when I try to run it from a different Python script using the egg, it can't seem to find the config files in the egg. If I put the config files into a directory relative to the calling Python script (external to the egg), it works - but that sort of defeats the purpose of having a self-contained egg that has all the functionality of the program and can be called from anywhere. I can use any classes/modules and run any functions from the egg as long as they don't use the config files... but if they do, the egg can't find them and so the functions don't work.

Any help would be really appreciated! We're kind of new to the egg thing here and don't really know where to start.

解决方案

The problem is, the config files are not files anymore - they're packaged within the egg. It's not easy to find the answer in the docs, but it is there. From the setuptools developer's guide:

Typically, existing programs manipulate a package's __file__ attribute in order to find the location of data files. However, this manipulation isn't compatible with PEP 302-based import hooks, including importing from zip files and Python Eggs.

To access them, you need to follow the instructions for the Resource Management API.

In my own code, I had this problem with a logging configuration file. I used the API successfully like this:

from pkg_resources import resource_stream

_log_config_file = 'logging.conf'
_log_config_location = resource_stream(__name__, _log_config_file)
logging.config.fileConfig(_log_config_location)
_log = logging.getLogger('package.module')

这篇关于在Python Egg中访问配置文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆