我正在从源代码库部署Google Cloud函数。只要我在main.py
中不引用存储库中的任何用户定义函数(即package
或package2
中的脚本/函数,除了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.py
和requirements.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.py
和 requirements.txt
移回根目录由于这个stackoverflow问题中的问题而无法正常工作。我需要一种方法在
gcloud functions deploy ...
语句期间导入用户定义的软件包/函数。我的main.py
使用package.script1.py
,package.script2.py
,package2.script3.py
和package2.script4.py
中的函数。如何实现?编辑/更新的信息
我确实在每个包(
package
和package2
)中都有__init__.py
文件,但它们目前是空白的。我尝试了以下
gcloud deploy...
选项:
main.py
andrequirements.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
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
The using the original file structure in this question (with
main.py
andrequirements.txt
inpackage
), I have tried the deploy statement provided above. It resolves themain.py
not found issue, but gives me the error as stated in the original question above
我还应该尝试什么?
__init__.py
文件吗? - Dustin Ingram