Firebase Functions 生成2的Python初始化不起作用。

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

Firebase functions gen2 python init does not work

问题

我系统中只安装了一个Python版本:3.10.10,其中包含最新的pip版本:23.1.2,我也安装了最新的firebase_functions模块。

在我尝试在我的机器上初始化Firebase函数时,我按照说明操作,但当它要求我安装依赖项时,我收到以下错误:

ERROR: 要修改pip,请运行以下命令:
C:\Users\XXX\functions\venv\Scripts\python.exe -m pip install --upgrade pip
Error: 发生了意外错误。

下一次我运行相同的过程,但这次我没有同意安装依赖项,它成功了:

Firebase初始化完成!

现在这是Google提供的默认代码:

# 欢迎使用Firebase的Cloud Functions for Python!
# 要开始,请简单地取消注释下面的代码或创建您自己的代码。
# 使用`firebase deploy`进行部署

from firebase_functions import https_fn
from firebase_admin import initialize_app

initialize_app()

@https_fn.on_request()
def on_request_example(req: https_fn.Request) -> https_fn.Response:
    return https_fn.Response("Hello world!")

我已安装了所有依赖项。我确保了成千上万次。
当我运行以下命令时:

firebase deploy

我收到以下错误:

i  正在部署函数
i  准备代码库以进行部署
i  确保已启用所需的API cloudfunctions.googleapis.com…
i  确保已启用所需的API cloudbuild.googleapis.com…
i  确保已启用所需的API artifactregistry.googleapis.com…
+  函数:已启用所需的API cloudbuild.googleapis.com
+  artifactregistry:已启用所需的API artifactregistry.googleapis.com
+  函数:已启用所需的API cloudfunctions.googleapis.com

错误:发生了意外错误。

这是firebase-debug.log中的日志:

[debug] [2023-06-11T13:05:29.172Z] stderr: ModuleNotFoundError: No module named 'firebase_functions'

[debug] [2023-06-11T13:05:29.182Z] Error: spawn "C:\Users\XXX\functions\venv\Scripts\activate.bat" ENOENT
at notFoundError (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-spawn\lib\enoent.js:6:26)
at verifyENOENT (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-spawn\lib\enoent.js:40:16)
at cp.emit (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-spawn\lib\enoent.js:27:25)
at ChildProcess._handle.onexit (node:internal/child_process:291:12)
[error] Error: An unexpected error has occurred.
英文:

I have only one python installed in my system: 3.10.10. it includes the latest pip: 23.1.2 and I installed the latest module of firebase_functions

After I try to init firebase functions in my machine I follow the instructions and when it asks me to install dependencies I get this error:

ERROR: To modify pip, please run the following command:
C:\Users\XXX\functions\venv\Scripts\python.exe -m pip install --upgrade pip
Error: An unexpected error has occurred.

Next time I run the same process but this time I did not accept to install dependencies and it worked:

 Firebase initialization complete!

Now this is the default code google provided:

# Welcome to Cloud Functions for Firebase for Python!
# To get started, simply uncomment the below code or create your own.
# Deploy with `firebase deploy`

from firebase_functions import https_fn
from firebase_admin import initialize_app

initialize_app()


@https_fn.on_request()
def on_request_example(req: https_fn.Request) -> https_fn.Response:
    return https_fn.Response("Hello world!")

I have all dependencies installed. I made sure thousand times.
When I run

firebase deploy

I get this error:

i  deploying functions
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
+  functions: required API cloudbuild.googleapis.com is enabled
+  artifactregistry: required API artifactregistry.googleapis.com is enabled
+  functions: required API cloudfunctions.googleapis.com is enabled

 Error: An unexpected error has occurred.

And this is the log in the firebase-debug.log

[debug] [2023-06-11T13:05:29.172Z] stderr: ModuleNotFoundError: No module named 'firebase_functions'

[debug] [2023-06-11T13:05:29.182Z] Error: spawn "C:\Users\XXX\functions\venv\Scripts\activate.bat" ENOENT
at notFoundError (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-spawn\lib\enoent.js:6:26)
at verifyENOENT (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-spawn\lib\enoent.js:40:16)
at cp.emit (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-spawn\lib\enoent.js:27:25)
at ChildProcess._handle.onexit (node:internal/child_process:291:12)
[error] Error: An unexpected error has occurred.

答案1

得分: 0

显然,Firebase会单独创建自己的Python依赖项,与您计算机上的Python版本分开存储在venv文件夹中。
要使其工作,请按照以下步骤操作:

  1. 运行以下命令进行初始化:

    firebase init
    
  2. 选择函数:Functions: 配置云函数目录及其文件

  3. 当它询问:是否要现在安装依赖项?(Y/n)时,选择 No

  4. 在函数项目内打开 cmd 然后执行以下命令:

    cd functions\venv\Scripts
    python.exe -m pip install --upgrade pip
    python.exe -m pip install firebase_functions
    cd ../../../
    
  5. 现在执行:

    firebase init functions
    
  6. 选择覆盖,然后执行以下操作:

    • 文件 functions/requirements.txt 已存在。是否覆盖?不
    • 文件 functions/.gitignore 已存在。是否覆盖?不
    • 文件 functions/main.py 已存在。是否覆盖?不
    • 是否要现在安装依赖项?是
  7. 最后执行:

    firebase deploy --only functions
    

这样应该可以完美运行。

英文:

Apparently firebase creates its own python dependency separately from your own python version in your machine. It is stored in the venv folder.
To make it work follow the following steps:

firebase init

Choose functions: Functions: Configure a Cloud Functions directory and its files

When it asks: Do you want to install dependencies now? (Y/n)

Choose No

Open cmd within the functions project and then:

cd functions\venv\Scripts
python.exe -m pip install --upgrade pip
python.exe -m pip install firebase_functions
cd ../../../

And now:

firebase init functions

Choose Overwrite, and then:

File functions/requirements.txt already exists. Overwrite? No
File functions/.gitignore already exists. Overwrite? No
File functions/main.py already exists. Overwrite? No
Do you want to install dependencies now? Yes

And now:

firebase deploy --only functions

And it should work perfectly

huangapple
  • 本文由 发表于 2023年6月11日 21:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450609.html
匿名

发表评论

匿名网友

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

确定