问题
I want to package a project that contains (and uses) template html files and distribuite it as an egg. Since I’m using tornadoweb, which requires file paths to point to html files, I can’t access the resources via stream and I really need the html files to be extracted when my program is running.
I’m having a look at setuptools and according to resource_filename
docs (bold is mine):
Sometimes, it is not sufficient to access a resource in string or stream form, and a true filesystem filename is needed. In such cases, you can use this method (or module-level function) to obtain a filename for a resource. If the resource is in an archive distribution (such as a zipped egg), it will be extracted to a cache directory, and the filename within the cache will be returned. If the named resource is a directory, then all resources within that directory (including subdirectories) are also extracted. If the named resource is a C extension or “eager resource” (see the
setuptools
documentation for details), then all C extensions and eager resources are extracted at the same time.
Which seems exactly what I need. However this is not what happens on my machine. My setup.py
contains the following line:
data_files = [('html', ['html/index.html'])]
And index.html
is actually included in my egg file. When I run python3 setup.py install
my project gets installed as a single zipped egg file. Unfortunately, when my program executes the following line:
html_path = resource_filename(__name__, "html")
I get the following return value:
/usr/local/lib/python3.2/dist-packages/myproj-0.1-py3.2.egg/EGG-INFO/scripts/html/
The problem is that myproj-0.1-py3.2.egg
is actually a zip file so this is not a valid path.
It’s strange because if I call pkg_resources.get_cache_path(‘myproj’)
I get the following path back:
/root/.python-eggs/myproj-tmp
But nothing is extracted there (yes, I’m running the program as root, but I’m just testing it).
Any idea why my html
directory is not extracted?
回答1:
Found the cause of the issue. As @erykson noted I was using the wrong directory.
After replacing
html_path = resource_filename(__name__, "html")
with
html_path = resource_filename(Requirement.parse("myproj"), "html")
everything works fine.
来源:https://stackoverflow.com/questions/22381146/pkg-resources-resource-filename-is-not-extracting-files