Forums

Error running WSGI application-

Hello Friends, sadly my bootcamp did not go over in-depth deployment lessons. My Api is running into the following errors and I am not sure how to troubleshoot. I followed the documentation to manually deploy. I am pretty sure I am not accurately configuring the WSGI files in my code's repor, and not setting the right path. From my logs i am receiving the following error:

2022-10-25 19:59:21,669: ***************************************************
202`enter code here`2-10-25 20:02:14,652: Error running WSGI application
2022-10-25 20:02:14,657: ModuleNotFoundError: No module named 'thoughts'
2022-10-25 20:02:14,657:   File "/var/www/cschadeck_pythonanywhere_com_wsgi.py", line 16, in <module>
2022-10-25 20:02:14,658:     application = get_wsgi_application()
2022-10-25 20:02:14,658: 
2022-10-25 20:02:14,658:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2022-10-25 20:02:14,658:     django.setup(set_prefix=False)
2022-10-25 20:02:14,658: 
2022-10-25 20:02:14,658:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/__init__.py", line 19, in setup
2022-10-25 20:02:14,658:     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2022-10-25 20:02:14,659: 
2022-10-25 20:02:14,659:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/conf/__init__.py", line 92, in __getattr__
2022-10-25 20:02:14,659:     self._setup(name)
2022-10-25 20:02:14,659: 
2022-10-25 20:02:14,659:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/conf/__init__.py", line 79, in _setup
2022-10-25 20:02:14,659:     self._wrapped = Settings(settings_module)
2022-10-25 20:02:14,659: 
2022-10-25 20:02:14,660:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/conf/__init__.py", line 190, in __init__
2022-10-25 20:02:14,660:     mod = importlib.import_module(self.SETTINGS_MODULE)

My current configuration on pythonanyware;s deployment is as follows

# +++++++++++ DJANGO +++++++++++
# To use your own Django app use code like this:
import os
import sys


path = '/home/CSchadeck/Thoughts-and-Feelings-API/thoughts/settings.py'
if path not in sys.path:
    sys.path.insert(0, path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'thoughts.settings'

## Uncomment the lines below depending on your Django version
###### then, for Django >=1.5:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
###### or, for older Django <=1.4
#import django.core.handlers.wsgi
#application = django.core.handlers.wsgi.WSGIHandler()

In my code my manage.py file is in my code's main directory. Here is that code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    """Run administrative tasks."""
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "thoughts.settings")
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == "__main__":
    main()

All my code for my API is in a thoughts folder which is also in the main directory.

wsgi.py.py code is:

"""
WSGI config for thoughts project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "thoughts.settings")

application = get_wsgi_application()

If it helps my settings.py code is:

"

""
Django settings for thoughts project.
Generated by 'django-admin startproject' using Django 4.1.2.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-bxb4*=78)7-xt7pn*0!m%7nik75@wd#cujtj6$&!jopj=z47a#"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    "corsheaders",
    'rest_framework',
    'thoughts',
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",

]

MIDDLEWARE = [
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

CORS_ALLOW_ALL_ORIGINS = True

CORS_ALLOW_METHODS = [
    'DELETE',
    'GET',
    'OPTIONS',
    'PATCH',
    'POST',
    'PUT',
]

CORS_ALLOW_HEADERS = [
    'accept',
    'accept-encoding',
    'authorization',
    'content-type',
    'dnt',
    'origin',
    'user-agent',
    'x-csrftoken',
    'x-requested-with',
]

CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS: True


ROOT_URLCONF = "thoughts.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

WSGI_APPLICATION = "thoughts.wsgi.application"


# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", },
    {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", },
    {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", },
]


# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

Thank you in advance for your help and patience for a new developer.

It looks like path should be /home/CSchadeck/Thoughts-and-Feelings-API not /home/CSchadeck/Thoughts-and-Feelings-API/thoughts/settings.py

Also quickly looking at your configuration ALLOWED_HOSTS should not be an empty list, but it should contain your domain ALLOWED_HOSTS = ["cschadeck.pythonanywhere.com"]

Good Morning again. The fixees above have moved me on and now receiving the next errors for my deployment:

2022-10-26 14:41:38,783: ***************************************************
2022-10-26 14:43:09,495: Error running WSGI application
2022-10-26 14:43:09,501: ModuleNotFoundError: No module named 'corsheaders'
2022-10-26 14:43:09,501:   File "/var/www/cschadeck_pythonanywhere_com_wsgi.py", line 16, in <module>
2022-10-26 14:43:09,501:     application = get_wsgi_application()
2022-10-26 14:43:09,501: 
2022-10-26 14:43:09,501:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2022-10-26 14:43:09,501:     django.setup(set_prefix=False)
2022-10-26 14:43:09,501: 
2022-10-26 14:43:09,501:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
2022-10-26 14:43:09,501:     apps.populate(settings.INSTALLED_APPS)
2022-10-26 14:43:09,501: 
2022-10-26 14:43:09,502:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/apps/registry.py", line 91, in populate
2022-10-26 14:43:09,502:     app_config = AppConfig.create(entry)
2022-10-26 14:43:09,502: 
2022-10-26 14:43:09,502:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/apps/config.py", line 193, in create
2022-10-26 14:43:09,502:     import_module(entry)
2022-10-26 14:43:09,502: ***************************************************
2022-10-26 14:43:09,502: If you're seeing an import error and don't know why,
2022-10-26 14:43:09,502: we have a dedicated help page to help you debug: 
2022-10-26 14:43:09,502: https://help.pythonanywhere.com/pages/DebuggingImportError/
2022-10-26 14:43:09,502: ***************************************************
2022-10-26 14:43:20,269: Error running WSGI application
2022-10-26 14:43:20,269: ModuleNotFoundError: No module named 'corsheaders'
2022-10-26 14:43:20,269:   File "/var/www/cschadeck_pythonanywhere_com_wsgi.py", line 16, in <module>
2022-10-26 14:43:20,269:     application = get_wsgi_application()
2022-10-26 14:43:20,270: 
2022-10-26 14:43:20,270:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2022-10-26 14:43:20,270:     django.setup(set_prefix=False)
2022-10-26 14:43:20,270: 
2022-10-26 14:43:20,270:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/__init__.py", line 24, in setup
2022-10-26 14:43:20,270:     apps.populate(settings.INSTALLED_APPS)
2022-10-26 14:43:20,270: 
2022-10-26 14:43:20,270:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/apps/registry.py", line 91, in populate
2022-10-26 14:43:20,270:     app_config = AppConfig.create(entry)
2022-10-26 14:43:20,270: 
2022-10-26 14:43:20,270:   File "/home/CSchadeck/.virtualenvs/Thoughts-virtualenv/lib/python3.10/site-packages/django/apps/config.py", line 193, in create
2022-10-26 14:43:20,270:     import_module(entry)
2022-10-26 14:43:20,271: ***************************************************
2022-10-26 14:43:20,271: If you're seeing an import error and don't know why,
2022-10-26 14:43:20,271: we have a dedicated help page to help you debug: 
2022-10-26 14:43:20,271: https://help.pythonanywhere.com/pages/DebuggingImportError/
2022-10-26 14:43:20,271: ***************************************************

I tried update my django cors headers and confirmed that in my settings.py its in my INSTALLED_APPS :

INSTALLED_APPS = [
    "corsheaders",
    'rest_framework',
    'thoughts',
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",

]

and the middleware is installed:

MIDDLEWARE = [
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

Thank you again for assistance. WIll continue to troubleshoot on my end

Good Afternoon,

Problem is solved and this ticket can be closed. If it is helpful to anyone else reading along here is what I did to correct the errors.

in the wsgi.py file I changes the following:

from:

os.environ ("DJANGO_SETTINGS_MODULE, thoughts.settings")

to:

os.environ ["DJANGO_SETTINGS_MODULE"] = "thoughts.settings"

I also made sure that my Django Rest Framework was on the most recent version both on my local environment and on my bash console on my app in pythonanywhere.
thank you fijl and pythonanywhere!

You need to install the modules that your code is trying to use: https://help.pythonanywhere.com/pages/InstallingNewModules/