pythondjangogunicornwsgi

Module installed but "ModuleNotFoundError: No module named <module_name>" while running gunicorn


I am trying to deploy this website using nginx. The site is configured correctly and this has been verified when I run python manage.py runserver. But when I run the same using gunicorn and wsgi, gunicorn --bind 0.0.0.0:8002 config.wsgi it lays out error: ModuleNotFoundError: No module named 'crispy_forms'.

Crispy_forms are installed and has been verified using pip freeze. Virtual enviornment is also enabled. 'crispy-forms' is also already added in INSTALLED_APPS in settings.py.

Following is my error code:

root@devvm:/tmp/tmc_site# gunicorn --bind 0.0.0.0:8002 config.wsgi
[2022-02-03 23:03:07 +0000] [6333] [INFO] Starting gunicorn 20.1.0
[2022-02-03 23:03:07 +0000] [6333] [INFO] Listening at: http://0.0.0.0:8002 (6333)
[2022-02-03 23:03:07 +0000] [6333] [INFO] Using worker: sync
[2022-02-03 23:03:07 +0000] [6335] [INFO] Booting worker with pid: 6335
[2022-02-04 04:03:07 +0500] [6335] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/workers/base.py", line 134, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/util.py", line 359, in import_app
    mod = importlib.import_module(module)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/tmp/tmc_site/config/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/usr/local/lib/python3.8/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/usr/local/lib/python3.8/dist-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.8/dist-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python3.8/dist-packages/django/apps/config.py", line 223, in create
    import_module(entry)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'crispy_forms'
[2022-02-04 04:03:07 +0500] [6335] [INFO] Worker exiting (pid: 6335)
[2022-02-03 23:03:07 +0000] [6333] [INFO] Shutting down: Master
[2022-02-03 23:03:07 +0000] [6333] [INFO] Reason: Worker failed to boot.

enter image description here

Here is my project structure, I have changed the foldername of configuration files to "config".

tmc_site
    -- .
    -- ..
    -- assets
    -- config
        -- asgi.py
        -- __init__.py
        -- __pycache__
        -- settings.py
        -- urls.py
        -- wsgi.py
    -- db.json
    -- db.sqlite3
    - .git
    -- .gitattributes
    -- .gitignore
    -- manage.py
    -- requirements.txt
    -- static
    -- venv
    -- website

Following are my wsgi related content in files:

settings.py: WSGI_APPLICATION = 'config.wsgi.application'

wsgi.py:

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
application = get_wsgi_application()

Any help would be appreciated.


Solution

  • You can see from the error message that the system installed gunicorn try to use the global Python environment in /usr/local/lib/python3.8/dist-packages/, not the project specific virtual environment in /tmp/tmc_site/venv directory.

    Install gunicorn in the venv virtual env, so after you activate it, the gunicorn --bind 0.0.0.0:8002 config.wsgi command will use the local one with the correct environment. You can also call the gunicorn directly from the virtual env via ./venv/bin/gunicorn.