Forums

Django 1.7 Python 3.4 - Module object has no attribute 'SessionAuthenticationMiddleware'

Here is the output from my error log:

2014-12-31 14:09:58,369 :Traceback (most recent call last):
2014-12-31 14:09:58,369 :  File "/usr/local/lib/python3.4/dist-packages/django/utils/module_loading.py", line 28, in import_by_path
2014-12-31 14:09:58,370 :    attr = getattr(module, class_name)
2014-12-31 14:09:58,370 :AttributeError: 'module' object has no attribute 'SessionAuthenticationMiddleware'
2014-12-31 14:09:58,370 :
2014-12-31 14:09:58,370 :During handling of the above exception, another exception occurred:
2014-12-31 14:09:58,370 :
2014-12-31 14:09:58,370 :Traceback (most recent call last):
2014-12-31 14:09:58,370 :  File "/bin/user_wsgi_wrapper.py", line 130, in __call__
2014-12-31 14:09:58,370 :    self.error_log_file.logger.exception("Error running WSGI application")
2014-12-31 14:09:58,370 :  File "/usr/lib/python3.4/logging/__init__.py", line 1296, in exception
2014-12-31 14:09:58,371 :    self.error(msg, *args, **kwargs)
2014-12-31 14:09:58,371 :  File "/usr/lib/python3.4/logging/__init__.py", line 1289, in error
2014-12-31 14:09:58,371 :    self._log(ERROR, msg, args, **kwargs)
2014-12-31 14:09:58,372 :  File "/usr/lib/python3.4/logging/__init__.py", line 1395, in _log
2014-12-31 14:09:58,372 :    self.handle(record)
2014-12-31 14:09:58,372 :  File "/usr/lib/python3.4/logging/__init__.py", line 1404, in handle
2014-12-31 14:09:58,373 :    if (not self.disabled) and self.filter(record):
2014-12-31 14:09:58,373 :  File "/usr/lib/python3.4/logging/__init__.py", line 692, in filter
2014-12-31 14:09:58,373 :    for f in self.filters:
2014-12-31 14:09:58,373 :  File "/bin/user_wsgi_wrapper.py", line 122, in __call__
2014-12-31 14:09:58,373 :    app_iterator = self.app(environ, start_response)
2014-12-31 14:09:58,373 :  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
2014-12-31 14:09:58,374 :    self.load_middleware()
2014-12-31 14:09:58,374 :  File "/usr/local/lib/python3.4/dist-packages/django/core/handlers/base.py", line 45, in load_middleware
2014-12-31 14:09:58,374 :    mw_class = import_by_path(middleware_path)
2014-12-31 14:09:58,374 :  File "/usr/local/lib/python3.4/dist-packages/django/utils/module_loading.py", line 31, in import_by_path
2014-12-31 14:09:58,374 :    error_prefix, module_path, class_name))
2014-12-31 14:09:58,374 :django.core.exceptions.ImproperlyConfigured: Module "django.contrib.auth.middleware" does not define a "SessionAuthenticationMiddleware" attribute/class

I am trying to get this working, using PythonAnywhere for the first time, so I need help.

I think you may have the same problem as this person. The solution is, in your WSGI file (linked from the "Web" tab) to replace this:

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

with this:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

BTW did you come here from a particular tutorial or something like that? Two people with the same problem on the same day suggests to me that we need to update something somewhere on the Internet that's giving out-of-date advice. Could just be coincidence, of course :-)

I don't believe that is the cause of the problem since your solution is exactly how I have implemented it in the WSGI file. I was following this tutorial from PythonAnywhere: https://www.pythonanywhere.com/wiki/VirtualEnvForNewerDjango. The other person you mentioned must have been following this: https://www.pythonanywhere.com/wiki/DjangoTutorial.

I am still having the problem. The Django default welcome page works when I comment out the line for Sessions and SessionsAuthenticationMiddleware in settings.py. However, I might need to use sessions in future apps, so a fix is required.

OK, thanks for the pointer.

From a bit of googling around, it looks like that problem is normally caused when you're running Django 1.6 with code for 1.7. I see from the traceback that you're using the system default Django for Python 3.4:

2014-12-31 14:09:58,369 :  File "/usr/local/lib/python3.4/dist-packages/django/utils/module_loading.py", line 28, in import_by_path

Are you using a virtualenv so that you can use 1.7? If so, are you sure you have code to activate it in the WSGI file, and are you sure Django 1.7 is installed for the right Python version inside the virtualenv?

If you're pretty sure that all of that is set up correctly, can I take a look at your files? I can see them from our side, but we never look without permission.

Ok, so here is my WSGI file:

activate_this = '/home/ksimeon/.virtualenvs/django17/bin/activate_this.py'
with open(activate_this) as f:
    code = compile(f.read(), activate_this, 'exec')
    exec(code, dict(__file__=activate_this))
import os
import sys

path = '/home/ksimeon/kushansimeon/'
if path not in sys.path:
    sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'kushansimeon.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

How do I check if Django 1.7 is installed for the right Python version inside the virtualenv?

Thanks! The WSGI file looks fine. To check what's installed for what in the virtualenv, look inside /home/ksimeon/.virtualenvs/virtualenvname/lib. There should be a pythonX.X directory for each version of Python you have stuff installed for inside the virtualenv. Django 1.7 for Python 3.4 would be in python3.4/site-packages. If it's not there, check whether you have any other Python versions, and if it's in that version's subdirectory instead.

Spot on assessment, Giles! The django package was in the python 2.7 site-packages subdirectory instead of python 3.4. However, I have run into another issue. I removed the 2.7 package from the virtualenv and tried installing Django again. I am getting a permission denied error when the installer tries to uninstall django 1.3.7 while installing django 1.7.1. The virtualenv is activated. How do I solve this?

My Bash Console

See the screenshot above for more details.

I solved the problem by deleting the Django17 virtualenv folder and creating a new virtualenv using the command mkvirtualenv django17 --python=/usr/bin/python3.4 and then running pip install django. Now I have the correct versions of Python and Django running and I can see the Django default welcome page. I will let you know if I run into other problems. Thanks, Giles!

:-)

Glad to help :-)