【问题标题】:Google Cloud App Engine Deploy Fail (Django)Google Cloud App Engine 部署失败 (Django)
【发布时间】:2021-09-25 22:32:32
【问题描述】:

我正在尝试部署我的应用,但我在网站上不断收到此错误:

错误:服务器错误 服务器遇到错误,无法完成您的请求。 请在 30 秒后重试。

这方面的错误日志是:

2021-07-15 12:36:04 default[20210715t122912]  [2021-07-15 12:36:04 +0000] [20] [INFO] Worker exiting (pid: 20)
2021-07-15 12:36:04 default[20210715t122912]  [2021-07-15 12:36:04 +0000] [23] [ERROR] Exception in worker process
2021-07-15 12:36:04 default[20210715t122912]  Traceback (most recent call last):    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/arbiter.py", line 583, 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 119, in init_process      self.load_wsgi()    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi      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 49, in load      return self.load_wsgiapp()    File "/layers/google.python.pip/pip/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 39, 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 358, in import_app      mod = importlib.import_module(module)    File "/opt/python3.9/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 855, in exec_module    File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed    File "/srv/main.py", line 1, in <module>      from booking.wsgi import application    File "/srv/booking/wsgi.py", line 12, in <module>      from django.core.wsgi import get_wsgi_application  ModuleNotFoundError: No module named 'django'

我已经有一个 main.py 文件:

ma​​in.py

from booking.wsgi import application

app = application

我还有一个 app.yaml 文件,内容如下:


runtime: python39

entrypoint: gunicorn -b :$PORT booking.wsgi

有什么想法吗?

【问题讨论】:

  • 它在错误 no module named 'django' 中说你确定你已经安装了 django 和你所有的包吗?

标签: python django google-app-engine google-cloud-platform


【解决方案1】:

正如 Jameel Hamdan 指出的那样,问题是您没有在 Google App Engine (GAE) 上安装要求。

解决方法是将所有要求放入requirements.txt 文件中,如here 所述。

另一件事是您正在使用app.yaml 中的入口点。这有两个问题:

  1. 您的入口点不正确。考虑到您设置它的方式,您的入口点应该是entrypoint: gunicorn -b :$PORT main:app。这样您就告诉gunicornmain.py 文件中查找app 对象。
  2. 由于您使用的是非默认入口点,因此您需要确保gunicorn 在您的requirements.txt 中,如GAE docs for entrypoint 中所述。我知道这句话有点含蓄:如果您不指定入口点字段,App Engine 还会自动将 gunicorn 添加到您的 requirements.txt 文件中。但我确定您需要 gunicorn在你的requirements.txt

旁注

由于您可能没有使用requirements.txt 文件,因此您也可能没有使用virtualenv 进行本地开发。您应该始终使用virtualenv。请参阅relevant documentation。现在最好习惯这样做,这样您就可以在不考虑下一个项目的情况下这样做。

请注意,对于我之前链接的requirements.txtGAE docs 中也建议这样做。


编辑

尝试将 app.yaml 更改为 Django 的 the default app.yaml

runtime: python39

handlers:
# This configures Google App Engine to serve the files in the app's static
# directory.
- url: /static
  static_dir: static/

# This handler routes all requests not caught above to your main app. It is
# required when static routes are defined, but can be omitted (along with
# the entire handlers section) when there are no static files defined.
- url: /.*
  script: auto

【讨论】:

  • 感谢您的建议。我现在收到一条错误消息,提示没有称为预订的模块。 Booking 是 django 项目中应用的名称。有没有办法访问 main.py 文件?
  • 我编辑了我的答案,如果这没有帮助,请通过quickstart 了解该设置与您的设置有何不同...
猜你喜欢
  • 2020-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-24
  • 1970-01-01
  • 2021-06-19
  • 1970-01-01
  • 2013-10-28
相关资源
最近更新 更多