使用 Cloud Source 存储库设置 Google Cloud Function 时出现 ModuleNotFoundError
ModuleNotFoundError when setting up Google Cloud Function with Cloud Source repository
我正在基于 BitBucket 存储库中的代码部署 Google Cloud Function。我已经将 BitBucket 帐户链接到 Google Cloud "Source Repositories" 并且 Google 函数可以找到 repo 等。问题是我的 main.py 函数需要调用多个在我的存储库中的其他 packages/modules 中运行。我的 main.py 文件顶部有一些简单的导入语句,如下所示:
import base64
import json
from datetime import datetime
from competitor_scrape.headless import headless_browser
...
前几行加载正常,但第 4 行(在我的 BitBucket 存储库中调用 module/function 的行)在我尝试定义 [=] 函数时导致 Google 函数出现此错误45=] 云函数与我存储库中的 main.py:
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/worker.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/worker.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 competitor_scrape.headless import headless_browser
ModuleNotFoundError: No module named 'competitor_scrape.headless'
它问我有没有"list[ed] all of the required modules in requirements.txt"。我可以列出我自己存储库中的模块吗?如果是这样,我应该怎么做?我还没有找到有关如何执行此操作的任何文档。
我当前的 requirements.txt 文件如下所示:
google-cloud-pubsub
numpy==1.14.5
pandas==0.22.0
psycopg2==2.7.4
selenium==3.4.3
geopy==1.11.0
googlemaps==2.5.1
ratelimiter==1.2.0
sqlalchemy==1.2.0
zeep==2.5.0
EDIT/UPDATE
我的存储库文件结构现在看起来像这样:
.
├── competitor_scrape
│ ├── __init__.py
│ └── headless.py
├── main.py
└── requirements.txt
headless_browser
是在 headless.py
中定义的函数。但是,此时 competitor_scrape
中的 __init__.py
是空的(它是由 PyCharm 自动生成的)。看来 __init__.py
可能是问题的根源。我应该如何填充该脚本,以便 competitor_scrape
中的 scripts/functions 可用于 Google 云函数?
您不应该在 requirements.txt
中包含模块 -- 这只是为了从 PyPI 安装依赖项。
像这样的导入语句:
from competitor_scrape.headless import headless_browser
意味着你应该有一个像这样的目录结构:
.
├── competitor_scrape
│ ├── __init__.py
│ └── headless
│ └── __init__.py
├── main.py
└── requirements.txt
并且在 competitor_scrape/headless/__init__.py
文件中,您应该有一个名为 headless_browser
的变量。
不幸的是,问题很简单。文件 headless.py
位于本地工作目录,但未添加到 Git 修订控制,因此它未在云端更新,因此无法被 GCP 找到。
我在 main.py 中导入 config.py 时遇到了几乎相同的问题。看起来 .gitignore 文件中的所有内容也被 gcloud 函数部署忽略了。我之前没有看到这个问题!
错误(.gitignore 中有 config.py):
ModuleNotFoundError: No module named 'config'
错误(删除.gitignore文件后):
ERROR: (gcloud.functions.deploy) Could not read ignore file [./.gitignore]: Unable to read file [./.gitignore]: [Errno 2] No such file or directory: './.gitignore'
成功(添加一个空的 .gitignore 文件后):
Deploying function (may take a while - up to 2 minutes)...done.
availableMemoryMb: 2048
entryPoint: handler
httpsTrigger:
url: https://europe-west1-my-project.cloudfunctions.net/my-function
labels:
deployment-tool: cli-gcloud
maxInstances: 10
...
当我添加一个 .gitignore 文件 而没有 config.py 时,一切都按预期进行,我能够部署云功能!所以看起来 .gitignore 条目被排除在部署之外。对于之前的部署,我认为这种行为应该是 .gcloudignore 的工作。
我正在基于 BitBucket 存储库中的代码部署 Google Cloud Function。我已经将 BitBucket 帐户链接到 Google Cloud "Source Repositories" 并且 Google 函数可以找到 repo 等。问题是我的 main.py 函数需要调用多个在我的存储库中的其他 packages/modules 中运行。我的 main.py 文件顶部有一些简单的导入语句,如下所示:
import base64
import json
from datetime import datetime
from competitor_scrape.headless import headless_browser
...
前几行加载正常,但第 4 行(在我的 BitBucket 存储库中调用 module/function 的行)在我尝试定义 [=] 函数时导致 Google 函数出现此错误45=] 云函数与我存储库中的 main.py:
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/worker.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/worker.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 competitor_scrape.headless import headless_browser
ModuleNotFoundError: No module named 'competitor_scrape.headless'
它问我有没有"list[ed] all of the required modules in requirements.txt"。我可以列出我自己存储库中的模块吗?如果是这样,我应该怎么做?我还没有找到有关如何执行此操作的任何文档。
我当前的 requirements.txt 文件如下所示:
google-cloud-pubsub
numpy==1.14.5
pandas==0.22.0
psycopg2==2.7.4
selenium==3.4.3
geopy==1.11.0
googlemaps==2.5.1
ratelimiter==1.2.0
sqlalchemy==1.2.0
zeep==2.5.0
EDIT/UPDATE
我的存储库文件结构现在看起来像这样:
.
├── competitor_scrape
│ ├── __init__.py
│ └── headless.py
├── main.py
└── requirements.txt
headless_browser
是在 headless.py
中定义的函数。但是,此时 competitor_scrape
中的 __init__.py
是空的(它是由 PyCharm 自动生成的)。看来 __init__.py
可能是问题的根源。我应该如何填充该脚本,以便 competitor_scrape
中的 scripts/functions 可用于 Google 云函数?
您不应该在 requirements.txt
中包含模块 -- 这只是为了从 PyPI 安装依赖项。
像这样的导入语句:
from competitor_scrape.headless import headless_browser
意味着你应该有一个像这样的目录结构:
.
├── competitor_scrape
│ ├── __init__.py
│ └── headless
│ └── __init__.py
├── main.py
└── requirements.txt
并且在 competitor_scrape/headless/__init__.py
文件中,您应该有一个名为 headless_browser
的变量。
不幸的是,问题很简单。文件 headless.py
位于本地工作目录,但未添加到 Git 修订控制,因此它未在云端更新,因此无法被 GCP 找到。
我在 main.py 中导入 config.py 时遇到了几乎相同的问题。看起来 .gitignore 文件中的所有内容也被 gcloud 函数部署忽略了。我之前没有看到这个问题!
错误(.gitignore 中有 config.py):
ModuleNotFoundError: No module named 'config'
错误(删除.gitignore文件后):
ERROR: (gcloud.functions.deploy) Could not read ignore file [./.gitignore]: Unable to read file [./.gitignore]: [Errno 2] No such file or directory: './.gitignore'
成功(添加一个空的 .gitignore 文件后):
Deploying function (may take a while - up to 2 minutes)...done.
availableMemoryMb: 2048
entryPoint: handler
httpsTrigger:
url: https://europe-west1-my-project.cloudfunctions.net/my-function
labels:
deployment-tool: cli-gcloud
maxInstances: 10
...
当我添加一个 .gitignore 文件 而没有 config.py 时,一切都按预期进行,我能够部署云功能!所以看起来 .gitignore 条目被排除在部署之外。对于之前的部署,我认为这种行为应该是 .gcloudignore 的工作。