firebase云函数内的模块与python:ModuleNotFoundError:没有名为“src”的模块

Bel*_*len 1 python firebase google-cloud-platform firebase-tools google-cloud-functions

我有一个这样的目录结构:

python-functions/
--src
---- | api/
-------- | __init__.py
-------- | main.py
---- | __init__.py
---- | main.py
Run Code Online (Sandbox Code Playgroud)

我试图在 src/main.py 中定义所有函数,但在其相应的文件夹中有实现

python-functions/
--src
---- | api/
-------- | __init__.py
-------- | main.py
---- | __init__.py
---- | main.py
Run Code Online (Sandbox Code Playgroud)
# src/main.py
import src.api.main as api

@https_fn.on_request(timeout_sec=300)
def status(req: https_fn.Request) -> https_fn.Response:
    return api.status(req)
Run Code Online (Sandbox Code Playgroud)

但是在部署时我收到此错误:

# Importing like: import src
ModuleNotFoundError: No module named 'src'
127.0.0.1 - - [18/May/2023 00:09:43] "GET /\_\_/functions.yaml HTTP/1.1" 500 -
Error: Failed to parse build specification:

- FirebaseError Expect manifest yaml to specify a version number
Run Code Online (Sandbox Code Playgroud)

或者这个:

# importing like: from .api.main import main
ImportError: attempted relative import with no known parent package

127.0.0.1 - - [18/May/2023 00:28:19] "GET /__/functions.yaml HTTP/1.1" 500 -

Error: Failed to parse build specification:
- FirebaseError Expect manifest yaml to specify a version number
Run Code Online (Sandbox Code Playgroud)

我尝试过其他导入方式,但仍然遇到相同的错误。

Bel*_*len 5

我能够通过关注 github 问题上的评论来解决这个问题 https://github.com/firebase/firebase-functions-python/issues/92#issuecomment-1549153623

# Adding this to the top of main.py works as a workaround, but it's not ideal:

import sys
from pathlib import Path

sys.path.insert(0, Path(__file__).parent.as_posix())

from test import base
Run Code Online (Sandbox Code Playgroud)