“Modules inside Firebase Cloud Functions with Python: ModuleNotFoundError: No module named ‘src'”

huangapple go评论56阅读模式
英文:

Modules inside firebase cloud functions with python: ModuleNotFoundError: No module named 'src'

问题

抱歉,你的要求是只返回翻译好的部分,以下是翻译的内容:

我有这样的目录结构:

--src
---- | api/
-------- | __init__.py
-------- | main.py
---- | __init__.py
---- | main.py

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

# 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)
# src/api/main.py
def status(req: https_fn.Request):
    return https_fn.Response("Hello", status=200)

但在部署时出现以下错误:

# 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

或者是这个错误:

# 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

我尝试了其他导入的方法,但仍然出现相同的错误。

英文:

I have a directory structure like this:

python-functions/
--src
---- | api/
-------- | __init__.py
-------- | main.py
---- | __init__.py
---- | main.py

I'm trying to define all my functions in src/main.py but have the implementations in its corresponding folder

# 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)
# src/api/main.py
def status(req: https_fn.Request):
    return https_fn.Response("Hello", status=200)

But when deploying I get this error:

# 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

or this one:

# 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

I've tried other ways of importing like but I still get the same errors.

答案1

得分: 5

我能够通过遵循该GitHub问题评论中的方法解决此问题
https://github.com/firebase/firebase-functions-python/issues/92#issuecomment-1549153623

# 将这部分添加到main.py的顶部可作为一种解决方法,但并不理想:

import sys
from pathlib import Path

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

from test import base
英文:

I was able to solve this by following this comment on a github issue
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

huangapple
  • 本文由 发表于 2023年5月18日 11:30:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定