Ledge
Ledge

Reputation: 43

whitenoise module not found in production

Error: I ran into this error when trying to deploy my Django app using GAE using these instructions GAE for Django: ModuleNotFoundError: No module named 'whitenoise'. The error occurs when starting the wsgi application. The development version of the app runs fine.

Attempted fixes: Wrapping the wsgi in whitenoise as mentioned here was done, whitenoise with django docs but it didn't make a difference. whitenoise 6.7.0 is downloaded and is in requirements.txt, and the necessary whitenoise middleware is in settings.py. Also the production app was started in a venv.

Question: How do I get wsgi to find my whitenoise (and wiki) module?. Or what is better approach to deploying or debugging my app?

Thank you in advance.

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 with the url hidden for this post

# 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




Logs

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'

File structure

 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

Upvotes: 0

Views: 121

Answers (1)

NoCommandLine
NoCommandLine

Reputation: 6323

  1. I don't see wsgi in your posted file structure and so am not sure exactly which of your static file folders you're referring to

  2. Given that GAE (production) doesn't have access to your file directory, I wonder if os.path.abspath is actually giving you what you want.

  3. To test locally and simulate something close to production, you need to run your app using dev_appserver.py. I tried running Whitenoise myself locally with dev_appserver.py (using a basic hello world app) and I had no problems (note that I didn't use os.path.abspath; instead I used the relative path to the static file folder). But given that app.yaml handles static files, I wonder if Whitenoise is actually being used.

  4. Your app.yaml will never route urls to the auto handler. Everything will always go to the static directory. This is because the url regex for both handlers are the same but the static directory handler comes first and so that will be picked. Also note that if you don't test locally with dev_appserver.py, your app.yaml is bypassed when you're running locally

Upvotes: 0

Related Questions