在Google Cloud Function中访问用户定义的程序包(ModuleNotFoundError:未命名模块...)

时间:2018-09-26 17:50:10

标签: python git google-cloud-platform package google-cloud-functions

我正在从源存储库部署Google Cloud功能。只要我在main.py中没有对存储库中用户定义的任何函数(即packagepackage2下方的main.py之外。

.
├── package
|   ├── __init__.py
|   ├── main.py
|   ├── requirements.txt
|   ├── script1.py
|   └── script2.py
├── package2
|   ├── __init__.py
|   ├── script3.py
|   └── script4.py
└── ...

我最近有2个关于此功能部署herehere的堆栈溢出问题。他们共同为我提供了一个解决方案,其中main.pyrequirements.txt位于一个程序包中,我以该程序包为源部署了Google Cloud Function。现在的问题是,我需要能够访问其他包/脚本中的函数/脚本。如果我在from package.script1 import foo中包含类似main.py的语句,则在部署函数时会收到类似以下的错误:

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function load error: Code in file main.py can't be loaded.
Did you list all required modules in requirements.txt?
Detailed stack trace: Traceback (most recent call last):
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/wo
rker.py", line 211, in check_or_load_user_function
    _function_handler.load_user_function()
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions_v1beta2/wo
rker.py", line 140, in load_user_function
    spec.loader.exec_module(main)
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/user_code/main.py", line 4, in <module>
    from package.script1 import foo
ModuleNotFoundError: No module named 'package'

我目前正在使用以下google函数deploy语句来部署此函数:

gcloud functions deploy NAME --source https://source.developers.google.com/projects/PROJECT_ID/repos/REPOSITORY_ID/moveable-aliases/BRANCH_NAME/paths/package/ --trigger-topic TOPIC

由于this stack overflow question中的问题,无法将main.pyrequirements.txt移回根目录。

我需要一种在gcloud functions deploy ...语句中导入用户定义的包/函数的方法。我的main.py使用了package.script1.pypackage.script2.pypackage2.script3.pypackage2.script4.py中的函数。如何做到这一点?

编辑/更新信息

每个软件包(__init__.pypackage)中确实都有package2个文件,但它们目前为空白/空。

我尝试了以下gcloud deploy...选项:

    使用以下deploy命令在根目录中的
  1. main.pyrequirements.txt(而不是上面/最初显示的配置)中:

    gcloud functions deploy NAME --source https://source.developers.google.com/projects/PROJECT_ID/repos/REPOSITORY_ID/moveable-aliases/BRANCH --trigger-topic TOPIC
    

    这会产生以下错误:

    ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function load error: File main.py that is expected to define function doesn't exist
    
  2. 为了明确地将根目录称为源,我还尝试了使用与#1相同的文件配置

    gcloud functions deploy NAME --source https://source.developers.google.com/projects/PROJECT_ID/repos/REPOSITORY_ID/moveable-aliases/BRANCH/paths/ --trigger-topic TOPIC
    

    它给了我与#1相同的错误:

    ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function load error: File main.py that is expected to define function doesn't exist
    
  3. 在此问题中使用原始文件结构(在main.py中使用requirements.txtpackage),我已经尝试了上面提供的deploy语句。它解决了main.py找不到的问题,但是给了我上面上面原始问题所述的错误

我还应该尝试什么?

1 个答案:

答案 0 :(得分:0)

您应该像这样构建应用程序:

RecordSource

然后在Forms!frmQASingleView.RecordSource = "qQATeam" DoCmd.OpenForm FormName:="frmQASingleView" View:=acNormal, WindowMode:=acWindowNormal 中,您可以执行以下操作:

.
├── main.py
├── package
│   ├── __init__.py
│   ├── script1.py
│   └── script2.py
├── package2
│   ├── __init__.py
│   ├── script3.py
│   └── script4.py
└── requirements.txt