GCP部署Python脚本遇ModuleNotFoundError:'google'模块缺失
在Google Cloud Platform部署包含from google.cloud import storage的Python脚本时,网页显示“Service Unavailable”,日志中出现503错误及ModuleNotFoundError: No module named 'google',但本地控制台运行脚本正常。已尝试升级/重装依赖包、将google包目录直接放入网页文件夹,仍无法解决,需实现正常导入该模块。
报错日志
Python栈追踪
Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker worker.init_process() File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/gthread.py", line 92, in init_process super().init_process() File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process self.load_wsgi() File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi self.wsgi = self.app.wsgi() File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi self.callable = self.load() File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load return self.load_wsgiapp() File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp return util.import_app(self.app_uri) File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 359, in import_app mod = importlib.import_module(module) File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1030, in _gcd_import File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 850, in exec_module File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed File "/app/main.py", line 2, in <module> from google.cloud import storage ModuleNotFoundError: No module named 'google'
HTTP错误提示
The request failed because either the HTTP response was malformed or connection to the instance had an error. Additional troubleshooting documentation can be found at: https://cloud.google.com/run/docs/troubleshooting#malformed-response-or-connection-error
requirements.txt内容
beautifulsoup4==4.11.1 cachetools==5.2.0 certifi==2022.9.24 charset-normalizer==2.1.1 click==8.1.3 colorama==0.4.5 Flask==2.1.2 google==3.0.0 google-api-core==2.10.2 google-auth==2.14.1 google-cloud-core==2.3.2 google-cloud-storage==2.6.0 google-crc32c==1.5.0 google-resumable-media==2.4.0 googleapis-common-protos==1.56.4 idna==3.4 itsdangerous==2.1.2 Jinja2==3.1.2 MarkupSafe==2.1.1 protobuf==4.21.9 pyasn1==0.4.8 pyasn1-modules==0.2.8 requests==2.28.1 rsa==4.9 six==1.16.0 soupsieve==2.3.2.post1 urllib3==1.26.12 Werkzeug==2.1.2
解决方案
1. 删除冲突依赖
你的requirements.txt中包含的google==3.0.0是旧版Google API集合包,和google-cloud-storage等现代GCP客户端库存在模块结构冲突,会导致导入失败。直接删除该行后重新部署。
2. 确认部署时依赖安装逻辑
确保requirements.txt放在代码根目录,GCP部署流程(如Cloud Run)会自动执行pip install -r requirements.txt。如果使用自定义构建脚本或Dockerfile,需手动添加依赖安装步骤。
3. 用自定义Dockerfile锁定环境(推荐)
如果默认构建环境存在版本或路径问题,编写Dockerfile明确指定环境:
FROM python:3.9-slim WORKDIR /app COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD ["gunicorn", "--bind", ":8080", "main:app"]
4. 本地模拟验证
部署前用Docker运行上述镜像,进入容器执行pip list,确认google-cloud-storage及相关google模块已正确安装,且模块结构正常。
内容的提问来源于stack exchange,提问作者AdAdAd

