生产中未找到白噪声模块

问题描述 投票:0回答:1

错误: 当我尝试使用 GAE 使用这些说明 GAE for Django 部署 Django 应用程序时,遇到了此错误:ModuleNotFoundError:没有名为“whitenoise”的模块。 启动 wsgi 应用程序时发生错误。该应用程序的开发版本运行良好。

尝试修复: 正如这里提到的,将 wsgi 包装在whitenoise中已经完成,whitenoise with django docs,但它没有什么区别。已下载whitenoise 6.7.0并位于requirements.txt中,必要的whitenoise中间件位于settings.py中。生产应用程序也是在 venv 中启动的。

问题: 我如何让 wsgi 找到我的whitenoise(和wiki)模块?或者部署或调试我的应用程序的更好方法是什么?

提前谢谢您。

wsgi.py

import os
import sys

from django.core.wsgi import get_wsgi_application
from whitenoise import WhiteNoise


# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.

PROJECT_PATH = os.path.abspath(os.path.dirname(os.path.split(__file__)[0]))
PROJECT_PARENT = os.path.abspath(os.path.split(PROJECT_PATH)[0])
sys.path.append(PROJECT_PATH)
sys.path.append(PROJECT_PARENT)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testproject.settings")
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


application = get_wsgi_application()

application = WhiteNoise(application, root=os.path.join(PROJECT_DIR, 'static'))

app.yaml,其中隐藏了这篇文章的 url

# app.yaml

runtime: python311
env: standard
entrypoint: gunicorn -b :$PORT testproject.wsgi

env_variables:
    APPENGINE_URL: my_url
    DJANGO_SETTINGS_MODULE: "testproject.settings"

handlers:
- url: /.*
  static_dir: static/

- url: /.*
  script: auto

runtime_config:
  python_version: 3




日志

2024-07-16 22:15:28 default[20240716t221126]             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap_external>", line 940, in exec_module
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2024-07-16 22:15:28 default[20240716t221126]    File "/workspace/testproject/wsgi.py", line 32, in <module>
2024-07-16 22:15:28 default[20240716t221126]      application = get_wsgi_application()
2024-07-16 22:15:28 default[20240716t221126]                    ^^^^^^^^^^^^^^^^^^^^^^
2024-07-16 22:15:28 default[20240716t221126]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2024-07-16 22:15:28 default[20240716t221126]      django.setup(set_prefix=False)
2024-07-16 22:15:28 default[20240716t221126]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/django/__init__.py", line 24, in setup
2024-07-16 22:15:28 default[20240716t221126]      apps.populate(settings.INSTALLED_APPS)
2024-07-16 22:15:28 default[20240716t221126]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/django/apps/registry.py", line 91, in populate
2024-07-16 22:15:28 default[20240716t221126]      app_config = AppConfig.create(entry)
2024-07-16 22:15:28 default[20240716t221126]                   ^^^^^^^^^^^^^^^^^^^^^^^
2024-07-16 22:15:28 default[20240716t221126]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/django/apps/config.py", line 178, in create
2024-07-16 22:15:28 default[20240716t221126]      mod = import_module(mod_path)
2024-07-16 22:15:28 default[20240716t221126]            ^^^^^^^^^^^^^^^^^^^^^^^
2024-07-16 22:15:28 default[20240716t221126]    File "/layers/google.python.runtime/python/lib/python3.11/importlib/__init__.py", line 126, in import_module
2024-07-16 22:15:28 default[20240716t221126]      return _bootstrap._gcd_import(name[level:], package, level)
2024-07-16 22:15:28 default[20240716t221126]             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
2024-07-16 22:15:28 default[20240716t221126]    File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked
2024-07-16 22:15:28 default[20240716t221126]  ModuleNotFoundError: No module named 'wiki'
2024-07-16 22:15:28 default[20240716t221126]  [2024-07-17 00:15:28 +0200] [14] [INFO] Worker exiting (pid: 14)
2024-07-16 22:15:28 default[20240716t221126]  [2024-07-16 22:15:28 +0000] [11] [ERROR] Worker (pid:14) exited with code 3
2024-07-16 22:15:28 default[20240716t221126]  [2024-07-16 22:15:28 +0000] [11] [ERROR] Shutting down: Master
2024-07-16 22:15:28 default[20240716t221126]  [2024-07-16 22:15:28 +0000] [11] [ERROR] Reason: Worker failed to boot.
2024-07-17 04:07:25 default[20240717t015723]  "GET / HTTP/1.1" 502
2024-07-17 04:07:25 default[20240717t015723]  [2024-07-17 04:07:25 +0000] [11] [INFO] Starting gunicorn 22.0.0
2024-07-17 04:07:25 default[20240717t015723]  [2024-07-17 04:07:25 +0000] [11] [INFO] Listening at: http://0.0.0.0:8081 (11)
2024-07-17 04:07:25 default[20240717t015723]  [2024-07-17 04:07:25 +0000] [11] [INFO] Using worker: sync
2024-07-17 04:07:25 default[20240717t015723]  [2024-07-17 04:07:25 +0000] [14] [INFO] Booting worker with pid: 14

2024-07-17 04:07:28 default[20240717t015723]  [2024-07-17 06:07:28 +0200] [14] [ERROR] Exception in worker process
2024-07-17 04:07:28 default[20240717t015723]  Traceback (most recent call last):    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker      worker.init_process()    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/gunicorn/workers/base.py", line 134, in init_process      self.load_wsgi()    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi      self.wsgi = self.app.wsgi()
2024-07-17 04:07:28 default[20240717t015723]                  ^^^^^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/gunicorn/app/base.py", line 67, in wsgi
2024-07-17 04:07:28 default[20240717t015723]      self.callable = self.load()
2024-07-17 04:07:28 default[20240717t015723]                      ^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2024-07-17 04:07:28 default[20240717t015723]      return self.load_wsgiapp()
2024-07-17 04:07:28 default[20240717t015723]             ^^^^^^^^^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2024-07-17 04:07:28 default[20240717t015723]      return util.import_app(self.app_uri)
2024-07-17 04:07:28 default[20240717t015723]             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/gunicorn/util.py", line 371, in import_app
2024-07-17 04:07:28 default[20240717t015723]      mod = importlib.import_module(module)
2024-07-17 04:07:28 default[20240717t015723]            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "/layers/google.python.runtime/python/lib/python3.11/importlib/__init__.py", line 126, in import_module
2024-07-17 04:07:28 default[20240717t015723]      return _bootstrap._gcd_import(name[level:], package, level)
2024-07-17 04:07:28 default[20240717t015723]             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap_external>", line 940, in exec_module
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2024-07-17 04:07:28 default[20240717t015723]    File "/workspace/testproject/wsgi.py", line 32, in <module>
2024-07-17 04:07:28 default[20240717t015723]      application = get_wsgi_application()
2024-07-17 04:07:28 default[20240717t015723]                    ^^^^^^^^^^^^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
2024-07-17 04:07:28 default[20240717t015723]      return WSGIHandler()
2024-07-17 04:07:28 default[20240717t015723]             ^^^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/django/core/handlers/wsgi.py", line 118, in __init__
2024-07-17 04:07:28 default[20240717t015723]      self.load_middleware()
2024-07-17 04:07:28 default[20240717t015723]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/django/core/handlers/base.py", line 40, in load_middleware
2024-07-17 04:07:28 default[20240717t015723]      middleware = import_string(middleware_path)
2024-07-17 04:07:28 default[20240717t015723]                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/django/utils/module_loading.py", line 30, in import_string
2024-07-17 04:07:28 default[20240717t015723]      return cached_import(module_path, class_name)
2024-07-17 04:07:28 default[20240717t015723]             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "/layers/google.python.pip/pip/lib/python3.11/site-packages/django/utils/module_loading.py", line 15, in cached_import
2024-07-17 04:07:28 default[20240717t015723]      module = import_module(module_path)
2024-07-17 04:07:28 default[20240717t015723]               ^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "/layers/google.python.runtime/python/lib/python3.11/importlib/__init__.py", line 126, in import_module
2024-07-17 04:07:28 default[20240717t015723]      return _bootstrap._gcd_import(name[level:], package, level)
2024-07-17 04:07:28 default[20240717t015723]             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 1126, in _find_and_load_unlocked
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
2024-07-17 04:07:28 default[20240717t015723]    File "<frozen importlib._bootstrap>", line 1140, in _find_and_load_unlocked
2024-07-17 04:07:28 default[20240717t015723]  ModuleNotFoundError: No module named 'whitenoise'

文件结构

 wikiward
    ├── docs
    │   ├── development
    │   └── tips
    ├── src
    │   └── wiki
    │       ├── conf
    │       ├── core
    │       ├── editors
    │       ├── locale
    │       ├── migrations
    │       ├── models
    │       ├── plugins
    │       ├── __pycache__
    │       ├── static
    │       ├── templates
    │       ├── templatetags
    │       └── views
    ├── testproject
    │   ├── chroma_db
    │   │   ├── 0aae09e0-5737-4147-a4a8-af1862d43f5b
    │   │   ├── 3dd02d2f-f076-49de-ab76-56387dcf8f5e
    │   │   ├── 52816e06-f9e5-4edd-9e29-572312e7087c
    │   │   ├── 569d1063-0ee1-4b79-8d8b-d5c3110157d9
    │   │   ├── 5fc4762c-2dfe-4cd8-8b0c-d615abb80300
    │   │   └── 6602c433-53ad-47bd-a81f-3597bd38eae7
    │   ├── fixtures
    │   ├── media
    │   │   └── wiki
    │   ├── __pycache__
    │   ├── settings
    │   │   └── __pycache__
    │   ├── static
    │   │   ├── admin
    │   │   ├── django-browser-reload
    │   │   ├── mptt
    │   │   └── wiki
    │   ├── templates
    │   ├── testproject
    │   └── theme
    │       ├── __pycache__
    │       ├── static_src
    │       └── templates
    ├── tests
    │   ├── core
    │   ├── plugins
    │   │   ├── attachments
    │   │   ├── editsection
    │   │   ├── globalhistory
    │   │   ├── images
    │   │   ├── links
    │   │   ├── macros
    │   │   ├── notifications
    │   │   ├── pymdown
    │   │   └── redlinks
    │   └── testdata
    │       └── migrations
    └── theme
        ├── __pycache__
        ├── static_src
        │   └── src
        └── templates
python django google-app-engine wsgi whitenoise
1个回答
0
投票
  1. 我在您发布的文件结构中没有看到

    wsgi
    ,因此不确定您到底指的是哪个静态文件文件夹

  2. 鉴于 GAE(生产)无法访问您的文件目录,我想知道

    os.path.abspath
    是否真的给了您您想要的东西。

  3. 要在本地测试并模拟接近生产的东西,您需要使用

    dev_appserver.py
    运行您的应用程序。我自己尝试过(使用基本的 hello world 应用程序),没有任何问题(请注意,我没有使用
    os.path.abspath
    ;相反,我使用了静态文件的相对路径)

  4. 您的

    app.yaml
    永远不会将 url 路由到
    auto
    处理程序。一切都将始终进入静态目录。这是因为两个处理程序的 url 正则表达式相同,但静态目录处理程序先出现,因此将被选中。另请注意,如果您不使用
    dev_appserver.py
    进行本地测试,则当您在本地运行时,您的
    app.yaml
    会被绕过

© www.soinside.com 2019 - 2024. All rights reserved.