我通过从此文件夹运行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文件视为不是的目录。
答案 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)
答案 1 :(得分:1)
egg文件是一个zip存档。您可以使用unzip命令查看内容。