资源在本地可用,但“没有这样的文件或目录”与 Poetry 一起打包和部署时

发布于 2025-01-17 03:46:24 字数 324 浏览 1 评论 0原文

我使用定义为常量的路径,例如 TF_CONSTS = 'consts/tf_keras_param_config.json' ,它们在开发过程中工作正常。

但是,当使用 Poetry 构建包(即 poetry build --format sdist)并部署时,即使 JSON 文件与 Python 脚本一起复制,这些引用也会变得无效:

FileNotFoundError: [Errno 2] No such file or directory: 'some_package/consts/tf_keras_param_config.json'

为什么?

I use paths defined as constants, e.g. TF_CONSTS = 'consts/tf_keras_param_config.json' and they work fine during development.

However, when the package is built with Poetry (i.e. poetry build --format sdist) and deployed, these references become invalid even though the JSON files are copied along with the Python scripts:

FileNotFoundError: [Errno 2] No such file or directory: 'some_package/consts/tf_keras_param_config.json'

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2025-01-24 03:46:24

相对路径在开发过程中工作正常,但是 - 当代码打包然后安装时 - 它们必须转换为绝对路径,并且必须动态确定这些路径(它们取决于已安装包的位置):

import pkg_resources

# necessary for the path to resolve properly when package is installed
TF_CONSTS = pkg_resources.resource_filename("some_package", "consts/tf_keras_param_config.json")

另请考虑 importlib.resources

此模块提供类似于 pkg_resources< 的功能/code> 基本资源访问,无需该包的性能开销。这使得读取包中包含的资源变得更加容易,并且语义更加稳定和一致。


Relative paths work fine during development, but - when the code is packaged and then installed - they must be translated to absolute ones and these must be determined dynamically (they depend on the location of the installed package):

import pkg_resources

# necessary for the path to resolve properly when package is installed
TF_CONSTS = pkg_resources.resource_filename("some_package", "consts/tf_keras_param_config.json")

Consider also importlib.resources:

This module provides functionality similar to pkg_resources Basic Resource Access without the performance overhead of that package. This makes reading resources included in packages easier, with more stable and consistent semantics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文