简体   繁体   中英

pkg_resources doesn't extract files from python egg

I created a python egg by running python setup.py bdist_egg from this folder:

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

in file setup.py , I have:

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

and in file sensor_display.py :

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

I obtain then the egg file SensorDisplay-0.1-py2.7.egg in folder SensorDisplay\\dist\\ . However, when I install the egg with easy_install and run the file C:\\Python27\\Scripts\\sensor_display.py , I obtain the following error:

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'

It seems function resource_filename doesn't extract the egg-file because the filename returned considers the egg-file as a directory which it is not.

I found the problem, I replaced

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

with

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

see also pkg_resources.resource_filename is not extracting files

egg file is a zip archive. You can look at the contents using unzip command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM