【问题标题】:How to deploy google cloud function when main.py and requirements.txt aren't in the same directorymain.py和requirements.txt不在同一目录下如何部署谷歌云功能
【发布时间】:2020-09-15 00:22:38
【问题描述】:

我的目录结构如下所示:

src/
    main.py
requirements.txt

由于似乎没有any configurability 围绕在哪里寻找requirements.txt,我已经辞职在外部目录中运行gcloud functions deploy,我希望--entry-point 标志可能会给我在src 内定位main.py 有一些回旋余地。不幸的是,它似乎只定义了一个方法名。

那么除了强制main.pyrequirements.txt 在同一个目录中之外,还有什么方法可以使这项工作?

作为参考,我尝试运行的命令和相应的错误消息:

gcloud functions deploy refresh_classes --entry-point main --runtime python37 --trigger-resource send_refresh --trigger-event google.pubsub.topic.publish --timeout 540s

Deploying function (may take a while - up to 2 minutes)...failed.
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. Error message: File main.py that is expected to define function doesn't exist

【问题讨论】:

  • 只是好奇,你想用“推荐”的目录结构来避免什么?或者您的项目需要以这种方式构建的原因是什么?
  • 另外,--source 选项允许您从默认(当前目录)更改源目录,但它仍然希望 main.pyrequirements.txt 文件都在该目录中。
  • @DustinIngram 老实说,我刚接触云功能,不知道有推荐的目录结构
  • 另外,我有一个在main.py 中引用的配置文件,这在云功能中也不允许吗? @DustinIngram
  • 您可以根据需要添加任意数量的其他文件。唯一的要求是 main.py 文件和 requirements.txt 文件(如果要安装依赖项),位于同一目录中。

标签: python google-cloud-platform google-cloud-functions gcloud


【解决方案1】:

单独配置gcloud 或没有其他源文件是不可能的。 Cloud Functions 需要一个 source 目录,其中包含 requirements.txtmain.py

【讨论】:

    【解决方案2】:

    我怀疑这种约定出现在 Cloud Functions 中,因为在其他语言(例如 Golang、Java)中,源目录(按照约定)足以描述源代码和项目描述符(go.mod、@987654322 @ 等)定义外部包。在 Python 中,这些可能会断开连接。

    您是否尝试过创建从./requirements.txt./src/requirements.txt 的链接?这可能不起作用,因为链接的源在“源”的上下文之外,但它可能会起作用。

    否则,你可能会倒霉。

    【讨论】:

      猜你喜欢
      • 2019-01-30
      • 2017-08-15
      • 2021-02-26
      • 2018-09-14
      • 1970-01-01
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多