我的系统中只安装了一个Python:
3.10.10
。它包括最新的点:23.1.2
并且我安装了firebase_functions
的最新模块
在我的机器中尝试初始化 firebase 功能后,我按照说明进行操作,当它要求我安装依赖项时,我收到此错误:
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.
下次我运行相同的过程,但这次我不接受安装依赖项,它起作用了:
Firebase initialization complete!
现在这是谷歌提供的默认代码:
# 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!")
我已安装所有依赖项。我确定了一千次。 当我跑步时
firebase deploy
我收到此错误:
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.
这是 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.
显然,firebase 与您计算机中自己的 python 版本分开创建自己的 python 依赖项。它存储在
venv
文件夹中。
要使其正常工作,请按照以下步骤操作:
firebase init
选择功能:
Functions: Configure a Cloud Functions directory and its files
当它询问时:
Do you want to install dependencies now? (Y/n)
选择
No
在函数项目中打开cmd,然后:
cd functions\venv\Scripts
python.exe -m pip install --upgrade pip
python.exe -m pip install firebase_functions
cd ../../../
现在:
firebase init functions
选择覆盖,然后:
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
现在:
firebase deploy --only functions
它应该可以完美工作
为了能够部署您的函数,您必须首先激活函数目录中的 venv(虚拟环境)
在项目文件夹内的终端中运行:
cd functions/venv/Scripts
通过运行激活 venv:
activate
如果您使用的是 VS Code 等常见文本编辑器,您将看到绿色文本(venv),表示您的虚拟环境已激活
现在您已经激活了虚拟环境,您应该安装您想要的软件包:在您的情况下,您应该运行(从任何地方):
pip install firebase_functions
您现在应该看到软件包正在下载在您的 venv 中
然后返回您的
functions
文件夹并运行
firebase deploy
您现在应该可以走了。