【问题标题】:How to deploy Google Cloud Function with shared local dependencies?如何部署具有共享本地依赖项的 Google Cloud Function?
【发布时间】:2021-10-01 10:02:49
【问题描述】:

在 Cloud Functions 上部署时,我在使用本地模块时遇到了一些问题 - 任何想法或最佳实践都将不胜感激!

我正在尝试将我的项目的一部分部署为云功能。它使用项目中的一些本地代码,这些代码与其他模块共享——我为此使用了绝对导入。我正在使用 Cloud Repository 进行部署,并在其中说明了函数所在的文件夹 (parent\cloud_function\)。问题是父包不适用于该设置。

这是项目结构的示例:

├── parent_repo
│   ├── parent
│   │   ├── __init__.py
│   │   ├── config.conf
│   │   ├── config.py
│   │   ├── cloud_function
│   │   │   ├── __init__.py
│   │   │   ├── main.py
│   │   │   └── requirements.txt
│   │   ├── shared_module
│   │   │   ├── __init__.py
│   │   │   ├── package1.py
│   │   │   └── package2.py
│   │   ├── other_module
│   │   │   ├── __init__.py
│   │   │   ├── some_script.py
│   │   │   └── another_script.py
│   │   └── utils.py


在parent.cloud_function.main.py 和parent.other_module.some_script.py 中我使用:

from parent.shared_module.package1 import some_func
from parent.shared_module.package2 importsome_class

访问共享代码。但是,当尝试在 Cloud Functions 上部署该功能时,由于我假设它只查看文件夹,因此 parent 模块不可用。

当然,我可以简单地将所有必需的代码嵌套在 cloud_function 文件夹中 - 但从项目的角度来看并不理想 - 因为该代码在其他资源之间共享,并且在逻辑上不属于那里。

有没有人知道如何更好地做到这一点?

提前致谢!

【问题讨论】:

    标签: python google-cloud-functions python-import


    【解决方案1】:

    很快 - 这很困难。

    这里是Python runtime - Specifying dependencies的描述-

    requirements.txt 文件或将本地依赖项与您的 功能

    你可能可以和Private dependencies in the Cloud Build一起玩

    Deploy a Python Cloud Function with all package dependencies SO 问题中提供了一些想法。

    【讨论】:

    • 谢谢@al-dann - 我会调查一下,完成后重新发布我的最终设置,以备将来参考。暂时转移到别的事情上,但最终会回来。再次感谢!
    • “pip install -t”解决方案看起来很奇怪,pip 不是为本地环境构建东西吗?如果我在 ARM 上而 GCP 不是 ARM 怎么办?
    猜你喜欢
    • 2020-08-02
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2018-12-02
    • 1970-01-01
    相关资源
    最近更新 更多