pkg_resources不从python egg中提取文件

时间:2016-02-04 10:22:41

标签: python egg pkg-resources

我通过从此文件夹运行python setup.py bdist_egg创建了一个python egg:

SensorDisplay/
--- sensor_display/
----- __init__.py
----- can.py
----- sensor_display.py
----- data/
--------- sensor_param.txt
--- setup.py

在文件setup.py中,我有:

 package_data = {'' : ['*.txt']},
 scripts = ['sensor_display/sensor_display.py','sensor_display/can.py']

并在文件sensor_display.py中:

PARAM_FILE = "data/sensor_display.txt"
param_file = pkg_resources.resource_filename("sensor_display", PARAM_FILE)
f = open(param_file,"r")

然后我获取文件夹SensorDisplay-0.1-py2.7.egg中的egg文件SensorDisplay\dist\。但是,当我使用easy_install安装egg并运行文件C:\Python27\Scripts\sensor_display.py时,我收到以下错误:

IOError: [Errno 2] No such file or directory: 'C:\\Python27\\lib\\site-packages\
\sensordisplay-0.1-py2.7.egg\\EGG-INFO\\scripts\\data\\sensor_param.txt'

似乎函数resource_filename不提取egg文件,因为返回的文件名将egg文件视为不是的目录。

2 个答案:

答案 0 :(得分:2)

我发现了问题,我换了

PARAM_FILE = "data/sensor_display.txt"
param_file = pkg_resources.resource_filename("sensor_display", PARAM_FILE)

PARAM_FILE = "sensor_display/data/sensor_display.txt"
param_file = pkg_resources.resource_filename(pkg_resources.Requirement.parse("SensorDisplay"), PARAM_FILE)

另见pkg_resources.resource_filename is not extracting files

答案 1 :(得分:1)

egg文件是一个zip存档。您可以使用unzip命令查看内容。