django.core.exceptions.ImproperlyConfigured: WSGI application 'application' could not be loaded
Asked Answered
S

30

36

The scenario is,

I cloned the Django code for OpenShift-V3 from here . When I ran the code with python manage.py runserver, I got this error:

django.core.exceptions.ImproperlyConfigured: WSGI application 'application' could not be loaded; Error importing module: 'application doesn't look like a module path

I didn't add anything to the code and the required packages are already satisfied.

Sorci answered 3/1, 2017 at 10:52 Comment(1)
Really suggest you use the sample Django applications I pointed out in #41432184 They haven't been reorganised and are basically what you get when using startproject. They also explain what was changed to make them align better with OpenShift V3 and how it works.Deckhand
H
17

Go to django-ex/project/settings.py

Change the line in settings.py as below

WSGI_APPLICATION = 'application' to WSGI_APPLICATION = 'wsgi.application'

That's it :(

Hamner answered 3/1, 2017 at 10:59 Comment(1)
this doesn't work for me. I am getting the same error and even tried to be more specific and added the specific project.wsgi.application but it still doesn't work.Symbolism
I
51

I used a middleware CorsMiddleware but forget to install it so after install, it works perfectly.

pip install django-cors-headers

So check something like it you may miss something like it.

Importunity answered 3/4, 2019 at 7:1 Comment(1)
This is the correct answer. Just double check all your middleware!Uphold
C
33

Read carefully, it might say "The above exception was the direct cause of the following exception: ...". And the "above exception" being you forgot to install whitehoise. Run pip install whitenoise, it worked for me.

Cannonball answered 30/12, 2019 at 11:13 Comment(2)
Or, in my case, I had upgraded Django (from 2.2.2 to 3.0.4) but hadn't upgraded whitenoise yet. Once I updated whitenoise (from 4.1.2 to 5.0.1) to the latest version, this error went away.Tavares
A new upgrade of whitenoise broke my application. There are explicit instructions to update your app here whitenoise.evans.io/en/stable/changelog.html#v4-0Noticeable
L
18

If you run django project locally for development, just remove WSGI_APPLICATION variable from settings.py module. It needs in prod/stage settings, for example settings_prod.py

Lightman answered 27/8, 2019 at 6:26 Comment(4)
This helped me to find the module that caused the issue. For me it were old paths for the middlewares.Involucel
Just like @Involucel said it really helped me to see which module is causing this issueCabbagehead
This help me to track down typo error in my middleware. ThanksShirley
Ditto. My issue was syntax on the middleware definitions. Thanks.Stocker
H
17

Go to django-ex/project/settings.py

Change the line in settings.py as below

WSGI_APPLICATION = 'application' to WSGI_APPLICATION = 'wsgi.application'

That's it :(

Hamner answered 3/1, 2017 at 10:59 Comment(1)
this doesn't work for me. I am getting the same error and even tried to be more specific and added the specific project.wsgi.application but it still doesn't work.Symbolism
F
7

Do you have Django Debug Toolbar

Remove it and check if the problem goes away. Possible occurences:

pip uninstall django-debug-toolbar

INSTALLED_APPS = [
    ...
    'debug_toolbar',
    ...
]

MIDDLEWARE = [
    ...
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    ...
]

Fannyfanon answered 23/7, 2019 at 23:49 Comment(0)
D
6

This worked for me

Install this if you didn't before:

pip install whitenoise

in settings.py add :

MIDDLEWARE = [
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Distressed answered 31/1, 2022 at 23:2 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Dropkick
The problem for me - on a totally different project - was to read the actual stack trace which indicates that whitenoise was missing.Imparity
D
5
pip install whitenoise

Although solved my problem. Generally produced while we are moving a project to different virtual enviroments. Sometimes we forgot to install the package & whitenoise just broke the application little bit, because no where it is mentioned that you are missing "whitenoise" module.

Doubleminded answered 23/10, 2020 at 19:58 Comment(0)
T
3

in settings.py file change as follows:

WSGI_APPLICATION = 'your_project_name.wsgi.application'

Trimble answered 10/10, 2018 at 6:36 Comment(1)
WSGI_APPLICATION = 'edx_django_utils.wsgi.application' I have done same but still getting the same error as django.core.exceptions.ImproperlyConfigured: WSGI application 'wsgi.application' could not be loaded; Error importing module.Overseas
T
3

note that any error in importing modules anywhere prior to starting the wsgi application will also prompt this message, so first look at the trace and start from the top in fixing issues.

I ported a Django app from python 2.7 to python3 and add all sorts of module import issues, not connected to this issue directly.

Tersanctus answered 12/7, 2019 at 5:16 Comment(0)
R
3

Check your middleware and delete error string

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
raise ImportError(
    ImportError: Module "django.middleware.locale" does not define a "LocaleMiddlewarez" attribute/class
)
Rhetorician answered 7/4, 2022 at 12:52 Comment(0)
M
2

I was using django-cors-headers, then I thought I haven't implemented cors in my project, so went ahead to install django-cors-middleware, then it's started giving the wsgi exception, so i checked the stack trace and I found out that it's django-cors-headers and django-cors-middleware conflicting each other. I had to uninstall django-cors-middleware but it's still gives the same exception, so uninstall django-cors-headers too then reinstall and everything works fine....

Menfolk answered 13/12, 2019 at 9:19 Comment(0)
U
2

Same problem.. I checked whether django-cors-middleware and django-cors-headers installed or not. I found those were not installed and then I installed them.

  1. python -m pip install django-cors-middleware
  2. python -m pip install django-cors-headers

Then,

  1. python manage.py migrate
  2. python manage.py runserver

Finally, it's worked....

Urano answered 4/12, 2020 at 18:3 Comment(0)
P
2

Go to settings.py :

  • In MIDDLEWARE, check whether you have added anything which is not working.
  • In INSTALLED_APPS, check whether you have added the apps or not. If not, add all the apps you have created for this project.

Example: I had this error today and was totally unaware why is this happening then after checking for a while, I have found a very silly mistake e.g. I have added my newly created app in the MIDDLEWARE instead of INSTALLED_APPS.

Preece answered 13/3, 2021 at 9:15 Comment(0)
N
1

In the latest version of Django, to add your app in installed apps in settings.py file, write -

[ ....,
'<appname>.apps.<Appname>Config',
]

where <Appname>Config is the name of the function in the apps.py file.

Nematic answered 19/4, 2021 at 5:30 Comment(0)
H
1

You may not have entered your module name correctly in the setting.py or you may have added it incorrectly in wrong place like middleware...

Hostess answered 13/4, 2022 at 6:14 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewGadgeteer
A
0

I tried all these solution and the one worked for me was this:

pip install django-htmlmin

This module was missing and not mentioned in requirements.txt Shows long error message that ends with wsgi application improperly configured. But somewhere in middle I could see "No module named 'htmlmin' I installed and it is resolved.

Ascetic answered 6/10, 2020 at 7:23 Comment(0)
C
0

maybe using middleware is the simple way to fix this , this problem mostly happens about cors Policy

Code:

class CorsMiddleware(object):
   def __init__(self, get_response):
      self.get_response = get_response

   def __call__(self, request):
      return self.get_response(request)

   def process_response(self,resp):
      resp["Access-Control-Allow-Origin"] = "*"
      return resp
Cata answered 16/2, 2021 at 12:40 Comment(0)
U
0

It happens when you insert something in MIDDLEWARE = [......] solution tire to put added latest addend things at top of middleware.

Underbody answered 13/3, 2021 at 5:28 Comment(0)
K
0

You may also get this error while adding your custom middleware. While adding it, make sure you so it like this: folder_contains_your_middleware_file.middleware_file.middleware_class

Below is my example:

'company_management.loginCheckMiddleware.LoginCheckMiddleware'
Kayak answered 2/7, 2021 at 5:20 Comment(0)
P
0

I also had the same problem, in spite of trying all the above solutions I was facing the same issue. I noticed that before this error I was getting the below error:

ModuleNotFoundError: No module named 'log_request_id'

To resolve this error I installed the django-log-request-id package using pip install django-log-request-id and both the issues got resolved.

Polyhydric answered 2/9, 2021 at 8:56 Comment(0)
A
0

I removed the custom middleware, but forgot to exclude it from MIDDLEWARES in the settings.py module, so be sure to verify that as well.

Amends answered 7/9, 2021 at 15:34 Comment(0)
L
0

Inspecting the logs, the same error was thrown due to a "WhitenoiseMiddleware" error, such as this.

The way to solve it was to make sure that WhiteNoiseMiddleware is placed directly after the Django SecurityMiddleware (if you are using it) and before all other middleware. In settings.py:

MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',  # <-- here
  # ...
]

Check the Django documentation for details.

Lathe answered 4/10, 2021 at 21:49 Comment(0)
A
0

It's working for me

 MIDDLEWARE = [
        'corsheaders.middleware.CorsMiddleware', # add only comma 
    ]

My CORS Configuration

ALLOWED_HOSTS=['*']
CROS_ORIGIN_ALLOW_ALL = True
Attainder answered 28/1, 2022 at 9:58 Comment(0)
M
0

As it turned out, in my case, the project dependency: django-request-logging was not installed. This was resulting in the same error

pipenv install

installed the dependency and fixed the issue for me.

Madriene answered 4/5, 2022 at 9:40 Comment(0)
C
0

make sure your app name written in

INSTALLED_APPS = [ 
...
'App_name', 
]
Colburn answered 7/5, 2022 at 14:32 Comment(0)
E
0

This is caused by a number of issues. Just confirm:

  1. If you are using gunicorn to server you django app ensure Whitenose is installed,and added to your middlewares and your gunicorn command lookes something like gunicorn APP-NAME.wsgi:application --bind 0.0.0.0:8000 --timeout 120 --workers 2 --log-level info and ensure your settings.py has something likeWSGI_APPLICATION = "APP-NAME.wsgi.application"
  2. If above is correct or you ain`t using gunicorn. Kindly check your middlewares. Most of the time you might have a middleware thats not installed that maybe blocking the server from starting.
Estienne answered 23/2, 2023 at 2:33 Comment(0)
F
0

Try removing 'django.contrib.auth.middleware.AuthenticationMiddleware' from MIDDLEWARE. This worked for me when I had similar issue.

Farrahfarrand answered 26/7, 2023 at 12:13 Comment(0)
M
0

I would be surprised if that's the case but personally I had the same error and the problem was just a typo in the middlewares, I wrote corseheaders (with an extra "e") instead of corsheaders

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    ...
]
Melisenda answered 24/8, 2023 at 17:36 Comment(0)
T
0

For Django 4.2 / Django 5.0 in settings.py I had to change

WSGI_APPLICATION = 'ubereats_tracker.wsgi.app'

to

WSGI_APPLICATION = 'ubereats_tracker.wsgi.application'
Toilette answered 9/2 at 13:15 Comment(0)
U
-1

Make sure you are in desired python Environment

Get the requirements.txt file or the python modules list, which are needed to execute django.

Install all the Modules and you shall be good to go.

Unpretentious answered 5/6, 2020 at 14:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.