Django ImportError: No module named middleware

我们两清 提交于 2019-12-05 05:36:14

Open up a python shell by running python manage.py shell in your project directory.

Run the following commands one at a time in the python shell:

>>> from corsheaders.middleware import CorsMiddleware
>>> from oauth2_provider.middleware import OAuth2TokenMiddleware
>>> from django.contrib.auth.middleware import SessionAuthenticationMiddleware

One of the lines should give you an error like the following:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
ImportError: No module named middleware

The line that gives you that error is the missing module that is giving you the problem.

To find the path where the modules are searched, do

>>> import sys; sys.path

Alternatively, if you don't know how to use the python shell, you could just remove the following lines in your settings.MIDDLEWARE_CLASSES one at a time until you don't get that error anymore:

'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'corsheaders.middleware.CorsMiddleware',
'oauth2_provider.middleware.OAuth2TokenMiddleware',

Just reinstall the package that gave you the error.

django.contrib.auth.middleware -> django

corsheaders.middleware -> corsheaders

oauth2_provider.middleware -> oauth2_provider

Make sure that you have all of the supporting packages installed where they can be found. I've run into problems where there are multiple Python interpreters installed, and was inadvertently running Django with one interpreter while installing packages with another. The other thing I would verify is that you have the same versions of the packages on both machines. Pay close attention to corsheaders.middleware.CorsMiddleware and oauth2_provider.middleware.OAuth2TokenMiddleware since they are not part of Django.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!