Forums

Error importing Middleware

I get this error:

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 47, in load_middleware
2013-12-10 14:32:14,627 :    raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
2013-12-10 14:32:14,627 :django.core.exceptions.ImproperlyConfigured: Error importing middleware django.middleware.clickjacking: "No module named clickjacking"

I'm new to all of this, so I am not really sure where my error is. I'd appreciate any help. Thanks!

[edited by admin: formatting]

Hmm, that looks like you might be trying to run a Django app set up for Django version 1.4 or higher in a default Django web application, which is 1.3.7.

I think your best bet would be to use a virtualenv. There are instructions on how to do that on this help page.

I created a simple django web app in the terminal of a virtual machine that runs ubuntu. It runs locally. I then pushed it to github, and pulled it back to pythonanywhere. Is there no way to get it to run on pythonanywhere?

Yes, it should work fine if you set up a virtualenv. The reason it's not super-easy is because the virtual machine is running a newer default version of Django than PythonAnywhere is.

But if you follow the instructions on the page I linked to down the the bit where it says "Start a new django project", then skip the Django project creation bit (because you've already got a project) and pick up the instructions again from the bit where it says "Now edit your wsgi file", everything should work fine.

Ok, great! Thank you.

Ok, I set-up the virtualenv. And I get this error:

File "/var/www/ashmbrown_pythonanywhere_com_wsgi.py", line 2, in <module>
2013-12-10 17:23:33,289 :    execfile(activate_this, dict(__file__=activate_this))
2013-12-10 17:23:33,289 :NameError: name 'execfile' is not defined

Here is my wsgi file:

activate_this = '/home/ashmbrown/.virtualenvs/django15/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

import os
import sys

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

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

Here is my tree:

UnderstandingDjango
├── LICENSE
├── README.md
├── blog
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── hosting.md
├── manage.py
├── netmag
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── templates
│   │   ├── base.html
│   │   └── blog
│   │       ├── index.html
│   │       └── post.html
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── netmag.db

I'm sorry, I am obviously not very good at this.

[edited by admin: formatting]

Not your fault at all! It's all a bit confusing when you're dealing with lots of different versions of different things on different platforms.

So this problem is because I didn't realise you were using Python 3. Python 3 got rid of the execfile command, and changed the language in various other ways besides. If you're sure you want to use Python 3, then replace the line that looks like this:

execfile(activate_this, dict(__file__=activate_this))

with code that looks like this:

with open(activate_this) as f:
    code = compile(f.read(), activate_this, 'exec')
    exec(code, dict(__file__=activate_this))

If you actually wanted to use Python 2, then you'll need to re-create the web app as a Python 2 one -- just delete it from the web tab (that won't delete your files) and then go through the web app creation wizard again.

[edited by admin -- the old activate_this / exec system is now deprecated -- check out the overview on the wiki of the new virtualenv system.]

Ok, so I think I changed it to use Python 2, but now I get this error: ImportError: Could not import settings 'UnderstandingDjango.netmag.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named UnderstandingDjango.netmag.settings

My settings.py folder is nested inside two folders UnderstandingDjango-->netmag-->settings.py. So I am not sure if this is causing issues.

I am starting think it might just be easier to start a new site from pythonanywhere directions. There is nothing too special on that project. Also, it has sqlite... will that cause issues?

Thanks so much for your help. I appreciate it.

We're almost there! If I understand correctly, you've put settings.py in /home/ashmbrown/UnderstandingDjango/netmag. Your wsgi file has this line:

path = '/home/ashmbrown/UnderstandingDjango'

That means that when it looks for the module UnderstandingDjango.netmag.settings it will look for the file /home/ashmbrown/UnderstandingDjango/UnderstandingDjango/netmag/settings.py, which obviously isn't there.

So what you need to do is change that path line so that it says this;

path = '/home/ashmbrown/'

Then it will look in the right place, and everything should work.

Hey, hopefully this is the last one... but... So is the last like part of the path to settings.py? I have it written as so:

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

But I am getting an error that says: "ImportError: Could not import settings 'UnderstandingDjango.netmag.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named UnderstandingDjango.netmag.settings"

Thanks!

[edited by admin: formatting]

Hmm, that should do it. What happens if you start a bash console, then in /home/ashmbrown/ (where you should be by default) run the following commands:

source virtualenvwrapper.sh
workon django15
cd UnderstandingDjango
python manage.py shell

...?

I get: Python 2.7.5+ (default, Sep 19 2013, 13:48:49) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole)

That sounds like it's all OK. Could you try reloading the web app (from the "Web" tab) and see if that fixes it?

So I tried to reload it and still get this error: ImportError: Could not import settings 'UnderstandingDjango.netmag.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named UnderstandingDjango.netmag.settings

Do I need to change my settings file?

Your wsgi file has lost the lines necessary to activate the virtualenv, so you're running with the default Django and not Django 1.5.