部署Flask API至GCP App Engine时遇main模块找不到错误
部署Flask API到GCP App Engine时遇到ModuleNotFoundError问题
错误信息
2023-05-16 07:49:30 default[20230516t084639] Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker worker.init_process() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/workers/gthread.py", line 92, in init_process super().init_process() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process self.load_wsgi() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgiapp self.wsgi = self.app.wsgi() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi self.callable = self.load() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load return self.load_wsgiapp() File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp return util.import_app(self.app_uri) File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/util.py", line 359, in import_app mod = importlib.import_module(module) File "/layers/google.python.runtime/python/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 984, in _find_and_load_unlocked ModuleNotFoundError: No module named 'main'
相关配置与代码
app.yaml
runtime: python39 entrypoint: gunicorn -b :$PORT main:app instance_class: F1 automatic_scaling: target_cpu_utilization: 0.65 min_instances: 1 max_instances: 10
main.py
from flask import Blueprint, jsonify, request, send_file, current_app, Flask from app.services.commitgraph import ( fetch_commit_count_per_day, plot_commit_count, RepoNotFoundError, NoCommitsFoundError, InvalidUsername, THEMES, TYPES, ) app = Flask(__name__) @app.route("/v1/commit-graph", methods=["GET"]) def get_commit_graph(): # 代码省略 pass if __name__ == "__main__": app.run(debug=True)
requirements.txt
blinker==1.6.2 certifi==2023.5.7 charset-normalizer==3.1.0 click==8.1.3 colorama==0.4.6 contourpy==1.0.7 cycler==0.11.0 Flask==2.3.2 fonttools==4.39.4 gunicorn==20.1.0 idna==3.4 importlib-metadata==6.6.0 importlib-resources==5.12.0 itsdangerous==2.1.2 Jinja2==3.1.2 kiwisolver==1.4.4 MarkupSafe==2.1.2 matplotlib==3.7.1 numpy==1.24.3 packaging==23.1 pandas==2.0.1 Pillow==9.5.0 pyparsing==3.0.9 python-dateutil==2.8.2 pytz==2023.3 requests==2.30.0 scipy==1.10.1 six==1.16.0 tzdata==2023.3 urllib3==2.0.2 Werkzeug==2.3.4 zipp==3.15.0
项目结构
project_root/ main.py app.yaml requirements.txt app/ __init__.py api/ __init__.py services/ commitgraph.py
解决方案
修正导入路径
main.py中错误地将services目录放在app子目录下导入,实际services与main.py同级,需修改导入语句:# 原语句 # from app.services.commitgraph import (...) # 修改为 from services.commitgraph import ( fetch_commit_count_per_day, plot_commit_count, RepoNotFoundError, NoCommitsFoundError, InvalidUsername, THEMES, TYPES, )添加services目录的__init__.py
在services目录下创建空的__init__.py文件,让Python将其识别为可导入模块:project_root/ services/ __init__.py commitgraph.py确认部署命令执行目录
必须在项目根目录下运行gcloud app deploy,确保GCP能找到main.py和app.yaml文件。验证entrypoint配置
当前app.yaml中的entrypointgunicorn -b :$PORT main:app是正确的,只要main.py在根目录且定义了app对象,无需修改。
内容的提问来源于stack exchange,提问作者Kyzoki
相关产品推荐
相关产品推荐

