gpt4 book ai didi

python-3.x - pkg_resources.resource_filename 未提取文件

转载 作者:行者123 更新时间:2023-12-01 23:58:53 25 4
gpt4 key购买 nike

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

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

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.



这似乎正是我所需要的。但是,这不是我机器上发生的情况。我的 setup.py包含以下行:
data_files = [('html', ['html/index.html'])]
index.html实际上包含在我的egg文件中。当我跑 python3 setup.py install我的项目被安装为一个单独的压缩蛋文件。不幸的是,当我的程序执行以下行时:
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 文件,所以这不是一个有效的路径。

这很奇怪,因为如果我调用 pkg_resources.get_cache_path(‘myproj’)我得到以下路径:
/root/.python-eggs/myproj-tmp
但是那里没有提取任何内容(是的,我以 root 身份运行该程序,但我只是在测试它)。

知道为什么我的 html目录没有解压?

最佳答案

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

更换后
html_path = resource_filename(__name__, "html")

html_path = resource_filename(Requirement.parse("myproj"), "html")
一切正常。

关于python-3.x - pkg_resources.resource_filename 未提取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22381146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com