在Google Cloud Function中访问用户自定义包(ModuleNotFoundError: No module named...)

9

我正在从源代码库部署Google Cloud函数。只要我在main.py中不引用存储库中的任何用户定义函数(即packagepackage2中的脚本/函数,除了main.py),就可以成功部署Google Function。

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

我有两个最近的Stack Overflow问题与此函数部署相关,分别在这里这里。它们共同引导我走向一个解决方案,即将main.pyrequirements.txt放在一个包中,并将该包作为源来部署Google Cloud Function。现在的问题是,我需要能够访问其他包/脚本中的函数/脚本。如果我在main.py中包含像from package.script1 import foo这样的语句,当我部署函数时会出现下面的错误:

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函数部署语句来部署此功能:
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

main.pyrequirements.txt 移回根目录由于这个stackoverflow问题中的问题而无法正常工作。
我需要一种方法在gcloud functions deploy ...语句期间导入用户定义的软件包/函数。我的main.py使用package.script1.pypackage.script2.pypackage2.script3.pypackage2.script4.py中的函数。如何实现?
编辑/更新的信息
我确实在每个包(packagepackage2)中都有__init__.py文件,但它们目前是空白的。
我尝试了以下gcloud deploy...选项:
  1. main.py and requirements.txt in the root directory (instead of the configuration as shown above/originally) with this deploy command:

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

    This produces the following error:

    ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function load error: File main.py that is expected to define function doesn't exist
    
  2. In an attempt to explicitly call out the root directory as source, I've also tried this with the same file configuration as in #1

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

    It gives me the same error as #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. The using the original file structure in this question (with main.py and requirements.txt in package), I have tried the deploy statement provided above. It resolves the main.py not found issue, but gives me the error as stated in the original question above

我还应该尝试什么?


你的每个包目录中都有__init__.py文件吗? - Dustin Ingram
很高兴收到你的来信,达斯汀!是的,我有这些,但它们仍然是空白的。我应该如何填充它们以使其工作? - Jed
1个回答

0
你应该按照以下方式构建你的应用程序:
.
├── main.py
├── package
│   ├── __init__.py
│   ├── script1.py
│   └── script2.py
├── package2
│   ├── __init__.py
│   ├── script3.py
│   └── script4.py
└── requirements.txt

然后在 main.py 中,你可以这样做:

from package.script1 import foo
from package2.script2 import bar
# etc

这正是我在此帖中构建项目的方式,但是谷歌云在查找main.py文件时出了问题。 - Jed
即使您从与 main.py 相同的目录部署,它还是会产生什么错误? - Dustin Ingram
你是指使用“ZIP上传”吗?在GUI Google Cloud Functions的“创建函数”页面上有4个选项:(1)内联编辑器,(2)Zip上传,(3)来自Cloud Storage的Zip,以及(4)Cloud Source存储库。 - Jed
不,我的意思是使用 gcloud 命令行工具直接部署,例如:gcloud functions deploy NAME --runtime python37 --trigger-topic TOPIC - Dustin Ingram
我尝试使用@DustinIngram的结构和命令部署我的函数,但是它产生了这个错误:ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. Error message: File main.py is expected to contain a function named FOO - lmaonuts
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接