本文介绍了pkg_resources.resource_filename未提取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打包一个包含(并使用)模板html文件的项目,并将其作为鸡蛋进行分发.由于我使用的是tornadoweb,它需要文件路径指向html文件,因此我无法通过流访问资源,并且确实需要在程序运行时提取html文件.

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.

我正在查看setuptools并根据resource_filename文档(粗体是我的):

I’m having a look at setuptools and according to resource_filename docs (bold is mine):

似乎正是我所需要的.但是,这不是我的机器上发生的情况.我的setup.py包含以下行:

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'])]

index.html实际上包含在我的egg文件中.当我运行python3 setup.py install时,我的项目将作为单个压缩的鸡蛋文件安装.不幸的是,当我的程序执行以下行时:

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")

我得到以下返回值:

/usr/local/lib/python3.2/dist-packages/myproj-0.1-py3.2.egg/EGG-INFO/scripts/html/

问题在于myproj-0.1-py3.2.egg实际上是一个zip文件,因此这不是有效路径.

The problem is that myproj-0.1-py3.2.egg is actually a zip file so this is not a valid path.

这很奇怪,因为如果我打电话给pkg_resources.get_cache_path(‘myproj’),我会得到以下路径:

It’s strange because if I call pkg_resources.get_cache_path(‘myproj’) I get the following path back:

/root/.python-eggs/myproj-tmp

但是在那里没有提取任何内容(是的,我以root用户身份运行该程序,但我只是对其进行测试).

But nothing is extracted there (yes, I’m running the program as root, but I’m just testing it).

知道为什么我的html目录没有被提取吗?

Any idea why my html directory is not extracted?

推荐答案

找到了问题的原因.正如@erykson所指出的,我使用了错误的目录.

Found the cause of the issue. As @erykson noted I was using the wrong directory.

更换后

html_path = resource_filename(__name__, "html")

使用

html_path = resource_filename(Requirement.parse("myproj"), "html")

一切正常.

这篇关于pkg_resources.resource_filename未提取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 17:31