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

Question:

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.

Asked By: user7139313

||

Answers:

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 🙁

Answered By: user5085948

in settings.py file
change as follows:

WSGI_APPLICATION = ‘your_project_name.wsgi.application’

Answered By: B.Hatuwal

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.

Answered By: Hasan

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.

Answered By: MrE

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',
    ...
]

Answered By: Ngatia Frankline

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

Answered By: Vladimir

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….

Answered By: unicdev

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.

Answered By: daggett

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.

Answered By: Abhishek Patil

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.

Answered By: Naazneen Jatu
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.

Answered By: Christopher Nolan

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….

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
Answered By: Ramin Azali

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

Answered By: Shah Vipul

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.

Answered By: Faizun Faria

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.

Answered By: Tejaswita Som

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'
Answered By: Yalchin Mammadli

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.

Answered By: Ravi Ranjan

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.

Answered By: JohnShallow

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.

Answered By: Jakob

It’s working for me

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

My CROS Configuration

ALLOWED_HOSTS=['*']
CROS_ORIGIN_ALLOW_ALL = True
Answered By: Sazib

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'
Answered By: Ömer ÇELEBİ

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
)
Answered By: Frig Mortu

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…

Answered By: Mahdi Kh

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.

Answered By: mansoor.khan

make sure your app name written in

INSTALLED_APPS = [ 
...
'App_name', 
]
Answered By: Ravindranath Avvaru

you only need to comment the " WSGI_APPLICATION = ‘app_name.wsgi.application’ " line in setting.py file ,
and now you can easily run your project.

Answered By: Happy Bishnoi

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.
Answered By: vinchuli