部署Python函数至Firebase服务器时遇SDK定位失败错误求助
运行firebase deploy上传Python云函数时遇到以下错误:
Error: Failed to find location of Firebase Functions SDK. Did you forget to run '. "/src/routes/functions/venv/bin/activate" && python3.11 -m pip install -r requirements.txt'?
我已完成以下操作:
- 运行
python3 -m venv venv创建虚拟环境 - 运行
source venv/bin/activate激活虚拟环境 - 运行
pip3 install -r requirements.txt安装依赖,requirements.txt内容为firebase_functions~=0.1.0
已安装的pip库列表:
Package Version ------------------------ --------- blinker 1.9.0 CacheControl 0.14.1 cachetools 5.5.0 certifi 2024.8.30 cffi 1.17.1 charset-normalizer 3.4.0 click 8.1.7 cloudevents 1.9.0 cryptography 43.0.3 deprecation 2.1.0 firebase-admin 6.6.0 firebase-functions 0.1.2 Flask 3.1.0 Flask-Cors 5.0.0 functions-framework 3.8.1 google-api-core 2.23.0 google-api-python-client 2.153.0 google-auth 2.36.0 google-auth-httplib2 0.2.0 google-cloud-core 2.4.1 google-cloud-firestore 2.19.0 google-cloud-storage 2.18.2 google-crc32c 1.6.0 google-events 0.13.0 google-resumable-media 2.7.2 googleapis-common-protos 1.66.0 grpcio 1.67.1 grpcio-status 1.67.1 gunicorn 23.0.0 httplib2 0.22.0 idna 3.10 itsdangerous 2.2.0 Jinja2 3.1.4 MarkupSafe 3.0.2 msgpack 1.1.0 packaging 24.2 pip 24.2 proto-plus 1.25.0 protobuf 5.28.3 pyasn1 0.6.1 pyasn1_modules 0.4.1 pycparser 2.22 PyJWT 2.9.0 pyparsing 3.2.0 PyYAML 6.0.2 requests 2.32.3 rsa 4.9 typing_extensions 4.12.2 uritemplate 4.1.1 urllib3 2.2.3 watchdog 6.0.0 Werkzeug 3.1.3
排查解决方法
匹配虚拟环境路径
错误提示的激活路径是/src/routes/functions/venv/bin/activate,检查你的项目结构是否与该路径一致。如果functions目录位置不同,调整激活命令的路径,确保Firebase能定位到正确的虚拟环境。指定Python版本安装依赖
错误提示使用python3.11,但你用的是python3/pip3,可能存在版本差异。激活虚拟环境后,执行:python3.11 -m pip install -r requirements.txt确认
python3.11在你的环境中可用,或替换为你实际使用的Python版本。检查Firebase配置
确保firebase.json的functions配置指向正确目录且runtime匹配Python版本:{ "functions": { "source": "./functions", "runtime": "python311" } }重新初始化functions目录
- 删除现有
venv目录 - 重新创建虚拟环境:
python3.11 -m venv venv - 激活虚拟环境:
source venv/bin/activate - 重装依赖:
pip install -r requirements.txt - 执行
firebase deploy --only functions
- 删除现有
验证SDK安装路径
激活虚拟环境后,运行:pip show firebase-functions确认输出的
Location路径在虚拟环境目录内(如venv/lib/python3.11/site-packages),若不在则说明依赖安装到了全局环境,需确保激活虚拟环境后再执行安装。
内容的提问来源于stack exchange,提问作者MattJ

